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

来源:互联网 发布:deepin linux安装教程 编辑:程序博客网 时间:2024/06/15 00:35

        字符串可以用单引号,双引号,三引号成对括起来,索引用[]界定,切片用[:]来省略和连接,这点类似于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 str2Python is cool!


0 0