您的位置:首页 > 编程语言 > Python开发

python字符串

2015-09-20 20:03 645 查看
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
'Python is cool!'
>>> pystr * 2
'PythonPython'
>>> '-' * 20
'--------------------'
>>> pystr = '''python
... is cool'''
>>> pystr
'python\nis cool'
>>> print pystr
python
is cool
>>>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: