Python字符串

Python中字符串被定义为引号之间的字符集合。python支持使用成对的单引号双引号,三引号(三个连续的单引号或者双引号)可以用来包含特殊的字符。使用索引操作符([])和切片操作符可以得到子字符串

第一个字符串索引是0,最后一个字符索引是-1.子字符串包含切片中的起始下标,但不包含结束下标

加号用于字符串的连接,星号用于字符串重复

>>> pystr = 'Python'
>>> iscool = "is cool!"
>>> pystr[0]
'P'
>>> pystr[2:5]
'tho'
>>> iscool[:2]
'is'
>>> iscool[3:]
'cool!'
>>> iscool[-1]
'!'
>>> pystr + iscool
'Pythonis cool!'
>>> pystr + '' + iscool
'Pythonis cool!'
>>> pystr + ' ' + iscool    #字符串的拼接
'Python is cool!'
>>> pystr*2          #字符串的重复操作
'PythonPython'
>>> '-' * 20
'--------------------'
>>> pystr = '''python 
... is cool!'''
>>> pystr
'python 
is cool!'
>>> print pystr
python 
is cool!

原文地址:https://www.cnblogs.com/weiwenbo/p/6552380.html