字符串
所有标准序列操作(索引、切片、乘法、成员资格检查、长度、最小值和最大值)都适用于字符串,但别忘了字符串是不可变的,因此所有的元素赋值和切片赋值都是非法的
>>> website = 'http://www.python.org'
>>> website[-3:] = 'com'
Traceback (most recent call last):
File "<pyshell#19>", line 1, in ?
website[-3:] = 'com'
TypeError: object doesn't support slice assignment
字符串格式
参考PDF文档
字符串方法
虽然字符串方法完全盖住了模块 string 的风头,但这个模块包含一些字符串没有的常量和函数
string.digits :包含数字0~9的字符串。
string.ascii_letters :包含所有ASCII字母(大写和小写)的字符串。
string.ascii_lowercase :包含所有小写ASCII字母的字符串。
string.printable :包含所有可打印的ASCII字符的字符串。
string.punctuation :包含所有ASCII标点字符的字符串。
string.ascii_uppercase :包含所有大写ASCII字母的字符串。
# 虽然说的是ASCII字符,但值实际上是未解码的Unicode字符串。
1. center 通过在两边添加填充字符(默认为空格)让字符串居中
>>> "The Middle by Jimmy Eat World".center(39)
' The Middle by Jimmy Eat World '
>>> "The Middle by Jimmy Eat World".center(39, "*")
'*****The Middle by Jimmy Eat World*****'
# 参考 ljust 、 rjust 和 zfill
2. find 在字符串中查找子串。如果找到,就返回子串的第一个字符的索引,否则返回 -1
>>> 'With a moo-moo here, and a moo-moo there'.find('moo')
7
>>> title = "Monty Python's Flying Circus"
>>> title.find('Monty')
0
>>> title.find('Python')
6
>>> title.find('Flying')
15
>>> title.find('Zirquss')
-1
#你还可指定搜索的起点和终点(它们都是可选的)。
>>> subject = '$$$ Get rich now!!! $$$'
>>> subject.find('$$$')
0
>>> subject.find('$$$', 1) # 只指定了起点
20
>>> subject.find('!!!')
16
>>> subject.find('!!!', 0, 16) # 同时指定了起点和终点
-1
# 参考 rfind 、 index 、 rindex 、 count 、 startswith 、 endswith
3. join 用于合并序列的元素 注意:所合并序列的元素必须都是字符串
>>> seq = [1, 2, 3, 4, 5]
>>> sep = '+'
>>> sep.join(seq) # 尝试合并一个数字列表
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: sequence item 0: expected string, int found
>>> seq = ['1', '2', '3', '4', '5']
>>> sep.join(seq) # 合并一个字符串列表
'1+2+3+4+5'
>>> dirs = '', 'usr', 'bin', 'env'
>>> '/'.join(dirs)
'/usr/bin/env'
>>> print('C:' + '\\'.join(dirs))
C:\usr\bin\env
4. lower 返回字符串的小写版本。
>>> 'Trondheim Hammer Dance'.lower()
'trondheim hammer dance'
>>> name = 'Gumby'
>>> names = ['gumby', 'smith', 'jones']
>>> if name.lower() in names: print('Found it!')
...
Found it!
>>>
# title 它将字符串转换为词首大写,即所有单词的首字母都大写,其他字母都小写
>>> "that's all folks".title()
"That'S All, Folks"
另一种方法是使用模块 string 中的函数 capwords 。
>>> import string
>>> string.capwords("that's all, folks")
That's All, Folks"
# 参考 islower 、 istitle 、 isupper 、 translate
5. extend 让你能够同时将多个值附加到列表末尾
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a.extend(b)
>>> a
[1, 2, 3, 4, 5, 6]
>>> a = [1, 2, 3]
>>> b = [4, 5, 6]
>>> a + b
[1, 2, 3, 4, 5, 6]
>>> a
[1, 2, 3]
# 拼接出来的列表与前一个示例扩展得到的列表完全相同,但在这里 a 并没有被修改
6. replace 指定子串都替换为另一个字符串,并返回替换后的结果
>>> 'This is a test'.replace('is', 'eez')
'Theez eez a test'
7. split 用于将字符串拆分为序列。
>>> '1+2+3+4+5'.split('+')
['1', '2', '3', '4', '5']
>>> '/usr/bin/env'.split('/')
['', 'usr', 'bin', 'env']
>>> 'Using the default'.split()
['Using', 'the', 'default']
# 参考:partition 、 rpartition 、 rsplit 、 splitlines
8. strip 将字符串开头和末尾的空白(但不包括中间的空白)删除,并返回删除后的结果。
>>> ' internal whitespace is kept '.strip()
'internal whitespace is kept'
判断字符串是否满足特定的条件
很多字符串方法都以 is 打头,如 isspace 、 isdigit 和 isupper ,
它们判断字符串是否具有特定的性质(如包含的字符全为空白、数字或大写)。
如果字符串具备特定的性质,这些方法就返回True ,否则返回 False 。
参考 isalnum isalpha isdecimal isdigit isidentifier islower isnumeric isprintable isspace istitle isupper