** http://www.runoob.com/python/python-strings.html
对于 python 来说,字符串是种强大的处理工具集,与 C 语言不同,python 没有“单一字符”这个类型,而是“可以使用一个字符的字符串”。直白的说,就是 python 里面的数据一旦成了字符串,不论高矮胖瘦多长多短永远都只是字符串。
一个字符串的排列顺序是无法被改变的,但是我们可以基于这些不能改变的顺序上去做两组字符串的结合,提取与检索。常见的字符串形式有如下几种:
a = '' # empty string
b = "spam's" # double quote
c = """ ... """ # triple quote, same as the two example above, but they can not be mixed
# this """ ... """ is really convenient for those who want to write plenty lines of strings as annotations.
d = r'\temp\spam' # raw string, indicating that all contents are JUST content
e = u'spam' # unicode string, coding based on unicode principle
# this is also called "宽字符串", occupying more space than the others
a + b # strings combination
c * 3 # repeat 3 times
a[i] # indexing a particular element
a[i:j] # slicing a range from i to j
len(b) # the length of a complete string
b.find('sp') # find the queued number in the string
b.strip() # to eliminate the space. REALLY USEFUL in practice
b.replace('sp', 'ab') # to replace first element by the second element
b.split(',') # to split elements with ',' customized signal
b.isdigit() # to test the content
b.lower() # transformation to short message
遇到空字符串的时候,python 并不会把字符串截断,而是保持整个字符串的长度和文本,还有顺序。(这点与 C 语言不同)
抑制转译
这个功能就是上面列举的 r'...' 的例子,有很多特定组合在 python 里面是有意义的,例如 "\n" 表示换行,"\t" 表示一个制表符号替代,为了避免暧昧不清,以下介绍两个方法:
1. 在字符串前面加一个 r ,如上面举例一般操作
2. 在产生误会的 "\" 本身 double 一下变成 "\\",像是 d = '\\temp\\spam'
索引与分片 Indexing and Slicing
偏移量从 0 开始计算,与 C 语言一样,不同的是 python 的偏移量 +- 皆宜!如下例子:
>>> s = 'spam'
>>> s[0], s[-2]
('s', 'a')
>>> s[1:3], s[1:], s[:-1]
('pa', 'pam', 'spa')
0 | 1 | 2 | 3 | -2 | -1 | ||||||
S | L | I | C | E | A | S | P | A | M | ||
[: | :] |
>>> s = 'abcdefg'
>>> s[1:6:2]
'bdf'
>>> s[::2]
'aceg'
>>> s[5:1:-1]
'fdec'
字符串转换工具
加减乘除可以运行的条件只有在彼此同个属性的情况才能发生,切记 '123' + 456 这样的方式是要出 error 的。为此做这类动作前,我们需要转换形态
o int() 转换 () 里面的东西成整数
o str() 转换 () 里面的东西成字符串
字符串里面还有分编码模式,有些情况可能要把对应的 ASCII 码对应出来,或是变回一般的字符串的话,可以有:
o ord() 转换 () 里面的东西成 ASCII 码
o chr() 转换 () 里面的东西回字符串
前面提到字符串虽然无法被修改,但是替换是没问题的,如上面的 .replace() function
>>> s = 'spam'
>>> s[0] = 'x'
# Raise an error! but we can do it this way...
>>> s = s.replace('pa', 'aaaaaa')
>>> s
'saaaaaam'
字串符的... Formalization(插入)
如果我们在一条 string 里面想插入一些东西,那么可以用“%”作为一个标记插入位置的符号,在 string 结束的右边直接用 %(..., ..., ...) 的模式排出安插的字元,其中 %?后面的搭配 ?字符也是一个学问:
1. s:string or any other objects
2. r:使用 repr,而非 str
3. c:字符
4. d:十进制整数
5. i:整数
6. u:无号,整数
7. %:常量
....... 还有很多可以被放在后面,就像是一种含义的名字,如下举例:
>>> x = 1234
>>> res = 'integers: ...%d...%-6d...%06d' %(x, x, x)
>>> res
'integers:...1234...1234 ...001234'
>>> x = 1.23456789
>>> '%e |%f |%g' %(x, x, x)
'1.234568e+000 |1.234568 |1.23457'
>>> '%-6.2f | %05.2f | %+06.1f' %(x, x, x)
'1.23 | 01.23 | +001.2'
# The upgraded edition of formalization: % + Dictionary
>>> '%(n)d %(x)s' %{'n':1, 'x':'spam'}
'1 spam'
>>> reply = """
Greetings...
Hello %(name)s!
Your age squared is %(age)s
"""
>>> values = {'name':'Bob', 'age':40}
>>> print(reply %values)
Greeting...
Hello Bob!
Your age squared is 40
尚有很多方法可以玩转字符串,它是个很基本,同时又很实用的一个技能!