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

2.4 python中的字符串、索引和截取 [python入门教程]

2014-01-27 23:15 686 查看
字符串可以用单引号,双引号,三引号成对括起来,索引用[]界定,切片用[:]来省略和连接,这点类似于Matlab。

>>> s1='Python'
>>> s2=' is cool!'
>>> str=s1+s2
>>> str
'Python is cool!'
>>> str[0]
'P'
>>> str[2:5]
'tho'
>>> str[:2]
'Py'
>>> str[3:]
'hon is cool!'
>>> str*2
'Python is cool!Python is cool!'
>>> '-'*8
'--------'
>>> str[-1]
'!'
字符串下标从0开始,最后一个字符的索引是-1。加号来连接,*用来重复。

>>> str2='Python\n is cool!'
>>> str2
'Python\n is cool!'
>>> print str2
Python
is cool!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: