# 字符串 ## 生成字符串 **Python**中可以使用一对单引号''或者双引号""生成字符串。 In [1]: ```py s = "hello, world" print s ``` ```py hello, world ``` In [2]: ```py s = 'hello world' print s ``` ```py hello world ``` ## 简单操作 加法: In [3]: ```py s = 'hello ' + 'world' s ``` Out[3]: ```py 'hello world' ``` 字符串与数字相乘: In [4]: ```py "echo" * 3 ``` Out[4]: ```py 'echoechoecho' ``` 字符串长度: In [5]: ```py len(s) ``` Out[5]: ```py 11 ``` ## 字符串方法 **Python**是一种面向对象的语言,面向对象的语言中一个必不可少的元素就是方法,而字符串是对象的一种,所以有很多可用的方法。 跟很多语言一样,**Python**使用以下形式来调用方法: ```py 对象.方法(参数) ``` ### 分割 s.split()将s按照空格(包括多个空格,制表符`\t`,换行符`\n`等)分割,并返回所有分割得到的字符串。 In [6]: ```py line = "1 2 3 4 5" numbers = line.split() print numbers ``` ```py ['1', '2', '3', '4', '5'] ``` s.split(sep)以给定的sep为分隔符对s进行分割。 In [7]: ```py line = "1,2,3,4,5" numbers = line.split(',') print numbers ``` ```py ['1', '2', '3', '4', '5'] ``` ### 连接 与分割相反,s.join(str_sequence)的作用是以s为连接符将字符串序列str_sequence中的元素连接起来,并返回连接后得到的新字符串: In [8]: ```py s = ' ' s.join(numbers) ``` Out[8]: ```py '1 2 3 4 5' ``` In [9]: ```py s = ',' s.join(numbers) ``` Out[9]: ```py '1,2,3,4,5' ``` ### 替换 s.replace(part1, part2)将字符串s中指定的部分part1替换成想要的部分part2,并返回新的字符串。 In [10]: ```py s = "hello world" s.replace('world', 'python') ``` Out[10]: ```py 'hello python' ``` 此时,s的值并没有变化,替换方法只是生成了一个新的字符串。 In [11]: ```py s ``` Out[11]: ```py 'hello world' ``` ### 大小写转换 s.upper()方法返回一个将s中的字母全部大写的新字符串。 s.lower()方法返回一个将s中的字母全部小写的新字符串。 In [12]: ```py "hello world".upper() ``` Out[12]: ```py 'HELLO WORLD' ``` 这两种方法也不会改变原来s的值: In [13]: ```py s = "HELLO WORLD" print s.lower() print s ``` ```py hello world HELLO WORLD ``` ### 去除多余空格 s.strip()返回一个将s两端的多余空格除去的新字符串。 s.lstrip()返回一个将s开头的多余空格除去的新字符串。 s.rstrip()返回一个将s结尾的多余空格除去的新字符串。 In [14]: ```py s = " hello world " s.strip() ``` Out[14]: ```py 'hello world' ``` s的值依然不会变化: In [15]: ```py s ``` Out[15]: ```py ' hello world ' ``` In [16]: ```py s.lstrip() ``` Out[16]: ```py 'hello world ' ``` In [17]: ```py s.rstrip() ``` Out[17]: ```py ' hello world' ``` ## 更多方法 可以使用dir函数查看所有可以使用的方法: In [18]: ```py dir(s) ``` Out[18]: ```py ['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill'] ``` ## 多行字符串 Python 用一对 `"""` 或者 `'''` 来生成多行字符串: In [19]: ```py a = """hello world. it is a nice day.""" print a ``` ```py hello world. it is a nice day. ``` 在储存时,我们在两行字符间加上一个换行符 `'\n'` In [20]: ```py a ``` Out[20]: ```py 'hello world.\nit is a nice day.' ``` ## 使用 `()` 或者 `\` 来换行 当代码太长或者为了美观起见时,我们可以使用两种方法来将一行代码转为多行代码: * () * \ In [21]: ```py a = ("hello, world. " "it's a nice day. " "my name is xxx") a ``` Out[21]: ```py "hello, world. it's a nice day. my name is xxx" ``` In [22]: ```py a = "hello, world. " \ "it's a nice day. " \ "my name is xxx" a ``` Out[22]: ```py "hello, world. it's a nice day. my name is xxx" ``` ## 强制转换为字符串 * `str(ob)`强制将`ob`转化成字符串。 * `repr(ob)`也是强制将`ob`转化成字符串。 不同点如下: In [23]: ```py str(1.1 + 2.2) ``` Out[23]: ```py '3.3' ``` In [24]: ```py repr(1.1 + 2.2) ``` Out[24]: ```py '3.3000000000000003' ``` ## 整数与不同进制的字符串的转化 可以将整数按照不同进制转化为不同类型的字符串。 十六进制: In [25]: ```py hex(255) ``` Out[25]: ```py '0xff' ``` 八进制: In [26]: ```py oct(255) ``` Out[26]: ```py '0377' ``` 二进制: In [27]: ```py bin(255) ``` Out[27]: ```py '0b11111111' ``` 可以使用 `int` 将字符串转为整数: In [28]: ```py int('23') ``` Out[28]: ```py 23 ``` 还可以指定按照多少进制来进行转换,最后返回十进制表达的整数: In [29]: ```py int('FF', 16) ``` Out[29]: ```py 255 ``` In [30]: ```py int('377', 8) ``` Out[30]: ```py 255 ``` In [31]: ```py int('11111111', 2) ``` Out[31]: ```py 255 ``` `float` 可以将字符串转换为浮点数: In [32]: ```py float('3.5') ``` Out[32]: ```py 3.5 ``` ## 格式化字符串 **Python**用字符串的`format()`方法来格式化字符串。 具体用法如下,字符串中花括号 `{}` 的部分会被format传入的参数替代,传入的值可以是字符串,也可以是数字或者别的对象。 In [33]: ```py '{} {} {}'.format('a', 'b', 'c') ``` Out[33]: ```py 'a b c' ``` 可以用数字指定传入参数的相对位置: In [34]: ```py '{2} {1} {0}'.format('a', 'b', 'c') ``` Out[34]: ```py 'c b a' ``` 还可以指定传入参数的名称: In [35]: ```py '{color} {n} {x}'.format(n=10, x=1.5, color='blue') ``` Out[35]: ```py 'blue 10 1.5' ``` 可以在一起混用: In [36]: ```py '{color} {0} {x} {1}'.format(10, 'foo', x = 1.5, color='blue') ``` Out[36]: ```py 'blue 10 1.5 foo' ``` 可以用`{:}`指定格式: In [37]: ```py from math import pi '{0:10} {1:10d} {2:10.2f}'.format('foo', 5, 2 * pi) ``` Out[37]: ```py 'foo 5 6.28' ``` 具体规则与C中相同。 也可以使用旧式的 `%` 方法进行格式化: In [38]: ```py s = "some numbers:" x = 1.34 y = 2 # 用百分号隔开,括号括起来 t = "%s %f, %d" % (s, x, y) ``` In [39]: ```py t ``` Out[39]: ```py 'some numbers: 1.340000, 2' ```