Home Backend Development Python Tutorial python开发之字符串string操作方法实例详解

python开发之字符串string操作方法实例详解

Jun 10, 2016 pm 03:07 PM
python python string string string How to operate

本文实例讲述了python开发之字符串string操作方法。分享给大家供大家参考,具体如下:

在python中,对于字符串string的操作,我们有必要了解一下,这样在我们的以后的开发中会给我们带来很多方便

下面是我学习的笔记:

#python-string
#python中的字符串用单引号''和双引号""标示
strA = 'this is a string'
strB = "this is a message!"
#打印两个字符串
print("打印两个字符串")
print('strA = ' + strA)
print('strB = ' + strB)
print("#############################")
strC = 'I don\'t know anything'
strD = '\'Yes\',I know.'
print("字符串中的转义字符")
print('strA = ' + strC)
print('strB = ' + strD)
print("#############################")
strE = '这是我的blog,欢迎大家来\n我的博客溜达'
print("字符串中的换行")
print('strA = ' + strE)
print("#############################")
strF = 'this is ''message'
strG = 'Hongten'
strH = strG * 3
print('字符串可以用\'+\'号连接(或者说粘合),也可以用\'*\'号循环')
print('strF原有形式为:\'this is \'\'message\'')
print('粘合后的strF:' + strF)
print('strG原值为:\'Hongten\',strH = strG * 3,此时strH为:' + strH)   
print("#############################")
strI = 'hongtenzone@foxmail.com'
print('字符串可以使用下标(索引)查询')
print('源字符串strI = \'hongtenzone@foxmail.com\'')
print('字符串strI的长度,len(strI) = ')
print(len(strI))
print('strI[0] = ' + strI[0])
print('strI[10] = ' + strI[10])
print('strI[-1] = strI[len(strI) - 1]')
print('strI[-1] = ' + strI[-1])
print('strI[len(strI) - 1] = ' + strI[len(strI) - 1])
print("#############################")
print('Python 字符串不能改写。按字符串索引赋值会产生错误:')
print('strI[0] = \'x\',这样就会产生错误啦')
print("#############################")
print('过大的索引代替为字符串大小,下界比上界大的返回空字符串')
print('strI[0:100] = ' + strI[0:100])
print("#############################")
print('索引可以是负数,计数从右边开始')
print('strI[-2] = ' + strI[-2])
print('strI[-23:] = ' + strI[-23:])
print("#############################")
print('不过-0 还是0,所以它不是从右边计数的!')
print('strI[0] = ' + strI[0])
print('strI[-0] = ' + strI[-0])

Copy after login

运行效果如下:

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
打印两个字符串
strA = this is a string
strB = this is a message!
#############################
字符串中的转移字符
strA = I don't know anything
strB = 'Yes',I know.
#############################
字符串中的换行
strA = 这是我的blog,欢迎大家来
我的博客溜达
#############################
字符串可以用'+'号连接(或者说粘合),也可以用'*'号循环
strF原有形式为:'this is ''message'
粘合后的strF:this is message
strG原值为:'Hongten',strH = strG * 3,此时strH为:HongtenHongtenHongten
#############################
字符串可以使用下标(索引)查询
源字符串strI = 'hongtenzone@foxmail.com'
字符串strI的长度,len(strI) = 
23
strI[0] = h
strI[10] = e
strI[-1] = strI[len(strI) - 1]
strI[-1] = m
strI[len(strI) - 1] = m
#############################
Python 字符串不能改写。按字符串索引赋值会产生错误:
strI[0] = 'x',这样就会产生错误啦
#############################
过大的索引代替为字符串大小,下界比上界大的返回空字符串
strI[0:100] = hongtenzone@foxmail.com
#############################
索引可以是负数,计数从右边开始
strI[-2] = o
strI[-23:] = hongtenzone@foxmail.com
#############################
不过-0 还是0,所以它不是从右边计数的!
strI[0] = h
strI[-0] = h
>>>
Copy after login

print打印字符串语句如下:

print('理解切片的最好方式是把索引视为两个字符之间的点,第一个字符的左边是0,字符串中第n个字符的右边是索引n')
print(' +---+---+---+---+---+ ')
print(' | H | e | l | p | A |')
print(' +---+---+---+---+---+ ')
print(' 0  1  2  3  4  5 ')
print('-5 -4 -3 -2 -1 -0')
print('第一行是字符串中给定的0到5各个索引的位置,第二行是对应的负索引。从i 到j 的切片由这两个标志之间的字符组成')
print('对于非负索引,切片长度就是两索引的差。例如,word[1:3] 的长度是2')

Copy after login

运行效果如下:

Python 2.7.9 (default, Dec 10 2014, 12:24:55) [MSC v.1500 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>> 
理解切片的最好方式是把索引视为两个字符之间的点,第一个字符的左边是0,字符串中第n个字符的右边是索引n
 +---+---+---+---+---+ 
 | H | e | l | p | A |
 +---+---+---+---+---+ 
 0  1  2  3  4  5 
-5 -4 -3 -2 -1 -0
第一行是字符串中给定的0到5各个索引的位置,第二行是对应的负索引。从i 到j 的切片由这两个标志之间的字符组成
对于非负索引,切片长度就是两索引的差。例如,word[1:3] 的长度是2
>>>

Copy after login

希望本文所述对大家Python程序设计有所帮助。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Can the Python interpreter be deleted in Linux system? Can the Python interpreter be deleted in Linux system? Apr 02, 2025 am 07:00 AM

Regarding the problem of removing the Python interpreter that comes with Linux systems, many Linux distributions will preinstall the Python interpreter when installed, and it does not use the package manager...

How to solve the problem of Pylance type detection of custom decorators in Python? How to solve the problem of Pylance type detection of custom decorators in Python? Apr 02, 2025 am 06:42 AM

Pylance type detection problem solution when using custom decorator In Python programming, decorator is a powerful tool that can be used to add rows...

How to solve permission issues when using python --version command in Linux terminal? How to solve permission issues when using python --version command in Linux terminal? Apr 02, 2025 am 06:36 AM

Using python in Linux terminal...

Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Python 3.6 loading pickle file error ModuleNotFoundError: What should I do if I load pickle file '__builtin__'? Apr 02, 2025 am 06:27 AM

Loading pickle file in Python 3.6 environment error: ModuleNotFoundError:Nomodulenamed...

Do FastAPI and aiohttp share the same global event loop? Do FastAPI and aiohttp share the same global event loop? Apr 02, 2025 am 06:12 AM

Compatibility issues between Python asynchronous libraries In Python, asynchronous programming has become the process of high concurrency and I/O...

What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? What should I do if the '__builtin__' module is not found when loading the Pickle file in Python 3.6? Apr 02, 2025 am 07:12 AM

Error loading Pickle file in Python 3.6 environment: ModuleNotFoundError:Nomodulenamed...

How to ensure that the child process also terminates after killing the parent process via signal in Python? How to ensure that the child process also terminates after killing the parent process via signal in Python? Apr 02, 2025 am 06:39 AM

The problem and solution of the child process continuing to run when using signals to kill the parent process. In Python programming, after killing the parent process through signals, the child process still...

See all articles