python字符串

来源:互联网 发布:51testing 软件测试 编辑:程序博客网 时间:2024/06/08 10:06

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 pystrpythonis cool>>>
0 0