码迷,mamicode.com
首页 > 编程语言 > 详细

python字符串

时间:2014-12-22 21:19:08      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:python   string   

http://blog.csdn.net/pipisorry/article/details/42085723

字符串

  Python除处理数字外还可以处理字符串,字符串用单撇号或双撇号包裹:
>>> ‘spam eggs‘
‘spam eggs‘
>>> ‘doesn/‘t‘
"doesn‘t"
>>> "doesn‘t"
"doesn‘t"
>>> ‘"Yes," he said.‘
‘"Yes," he said.‘
>>> "/"Yes,/" he said."
‘"Yes," he said.‘
>>> ‘"Isn/‘t," she said.‘
‘"Isn/‘t," she said.‘
>>>

字符串输出格式与输入的样子相同,都是用撇号包裹,撇号和其它特殊字符用用反斜杠转义。如果字符串中有单撇号而没有双撇号则用双撇号包裹,否则应该用单撇号包裹。后面要介绍的print语句可以不带撇号或转义输出字符串。

 字符串可以用+号连接起来,用*号重复:

>>> word = ‘Help‘ + ‘A‘
>>> word
‘HelpA‘
>>> ‘<‘ + word*5 + ‘>‘
‘<HelpAHelpAHelpAHelpAHelpA>‘
>>>

字符串可以象在C中那样用下标索引,字符串的第一个字符下标为0。

Python没有单独的字符数据类型,一个字符就是长度为一的字符串。象在Icon语言中那样,可以用片段(slice)记号来指定子串,片段即用冒号隔开的两个下标。

>>> word[4]
‘A‘
>>> word[0:2]
‘He‘
>>> word[2:4]
‘lp‘
>>>

片段有很好的缺省值:第一下标省略时缺省为零,第二下标省略时缺省为字符串的长度。

>>> word[:2]    # 前两个字符
‘He‘
>>> word[2:]    # 除前两个字符串外的部分
‘lpA‘
>>>

注意s[:i] + s[i:] 等于 s 是片段运算的一个有用的恒等式。

>>> word[:2] + word[2:]
‘HelpA‘
>>> word[:3] + word[3:]
 
‘HelpA‘
>>>

不合理的片段下标可以很好地得到解释:过大的下标被换成字符串长度,上界小于下界时返回空串。

>>> word[1:100]
‘elpA‘
>>> word[10:]
‘‘
>>> word[2:1]
‘‘
>>>

下标允许为负数,这时从右向左数。例如:

>>> word[-1]     # 最后一个字符
‘A‘
>>> word[-2]     # 倒数第二个字符
‘p‘
>>> word[-2:]    # 最后两个字符
‘pA‘
>>> word[:-2]    # 除最后两个字符外的部分
‘Hel‘
>>>

但要注意的是 -0 实际还是 0,所以它不会从右向左数!

>>> word[-0]     # (因为 -0 等于 0)
‘H‘
>>>

超出范围的片段下标被截断,但在非片段的情况下不要这样:

>>> word[-100:]
‘HelpA‘
>>> word[-10]    # 错误
Traceback (innermost last):
  File "<stdin>", line 1
IndexError: string index out of range
 
>>>

记住片段意义的最好方法是把下标看成是字符 之间的点,第一个字符的左边界号码为0。有n个字符的字符串的最后一个字符的右边界下标为n,例如:

 
 +---+---+---+---+---+ 
 | H | e | l | p | A |
 +---+---+---+---+---+ 
 0   1   2   3   4   5 
-5  -4  -3  -2  -1
 

第一行数字给出字符串中下标0到5的位置,第二行给出相应的负下标。从i到j的片段由在边界i和j之间的字符组成。

 

对于非负下标,如果下标都在界内,则片段的长度为下标的差。例如,word[1:3] 的长度为 2。

 内置函数len()返回字符串的长度:

>>> s = ‘supercalifragilisticexpialidocious‘
>>> len(s)
34
>>>

多行的长字符串也可以用行尾反斜杠续行,续行的行首空白不被忽略,如

        hello = "This is a rather long string containing/n        several lines of text just as you would do in C./n            Note that whitespace at the beginning of the line is         significant./n"
        print hello

结果为

 

        This is a rather long string containing
        several lines of text just as you would do in C.
            Note that whitespace at the beginning of the line is significant.

对于特别长的字符串(比如包含说明的几段文字),如果用上面的方式每行都用/n/结尾是很麻烦的,特别是这样无法用象Emacs这样的功能强大的编辑器重新编排。对这种情况,可以使用三重撇号,例如

        hello = """
 
            This string is bounded by triple double quotes (3 times ").
        Unescaped newlines in the string are retained, though         it is still possible/nto use all normal escape sequences.
 
            Whitespace at the beginning of a line is
        significant.  If you need to include three opening quotes
        you have to escape at least one of them, e.g. /""".
 
            This string ends in a newline.
        """

三重撇号字符串也可以用三个单撇号,没有任何语义差别。

多行的字符串常量可以直接连接起来,字符串常量之间用空格分隔则在编译时可以自动连接起来,这样可以把一个长字符串连接起来而不需要牺牲缩进对齐或性能,不象用加号连接需要运算,也不象字符串串内的换行其行首空格需要保持。


from:http://blog.csdn.net/pipisorry/article/details/42085723

ref:http://blog.csdn.net/wwwunix/article/details/4231


python字符串

标签:python   string   

原文地址:http://blog.csdn.net/pipisorry/article/details/42085723

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!