String

string模块中可以使用的全局变量
1. >>> import string
2. >>> string.digits
3. '0123456789'
4. >>> string.hexdigits
5. '0123456789abcdefABCDEF'
6. >>> string.octdigits
7. '01234567'
8. >>> string.letters
9. 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
10. >>> string.lowercase
11. 'abcdefghijklmnopqrstuvwxyz'
12. >>> string.uppercase
13. 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
14. >>> string.printable
15. '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~ \t\n\r\x0b\x0c'
16. >>> string.punctuation
17. '!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
18. >>> string.whitespace
19. '\t\n\x0b\x0c\r '
20. >>>

string.atof(s)将字符串转为浮点型数字
1. >>> string.atof("1.23")
2. 1.23
3. >>> string.atof("1")
4. 1.0

string.atoi(s,[base=num])将字符串转为整型数字,base 指定进制
1. >>> string.atoi("20")
2. 20
3. >>> string.atoi("20",base=10)
4. 20
5. >>> string.atoi("20",base=16)
6. 32
7. >>> string.atoi("20",base=8)
8. 16
9. >>> string.atoi("20",base=2)
10. Traceback (most recent call last):
11.   File "", line 1, in <module>
12.   File "/usr/lib64/python2.6/string.py", line 403, in atoi
13.     return _int(s, base)
14. ValueError: invalid literal for int() with base 2: '20'
15. >>> string.atoi("101",base=2)
16. 5
17. >>> string.atoi("101",base=6)
18. 37

string.capwords(s,sep=None)sep作为分隔符,分割字符串s,然后将每个字段的首字母换成大写
1. >>> string.capwords("this is a dog")
2. 'This Is A Dog'
3. >>> string.capwords("this is a dog",sep=" ")
4. 'This Is A Dog'
5. >>> string.capwords("this is a dog",sep="s")
6. 'This is a dog'
7. >>> string.capwords("this is a dog",sep="o")
8. 'This is a doG'
9. >>>

string.maketrans(s,r)创建一个sr的转换表,然后可以使用translate()方法来使用
1. >>> replist=string.maketrans("123","abc")
2. >>> replist1=string.maketrans("456","xyz")
3. >>> s="123456789"
4. >>> s.translate(replist)
5. 'abc456789'
6. >>> s.translate(replist1)
'123xyz789'