Chinaunix首页 | 论坛 | 博客
  • 博客访问: 907398
  • 博文数量: 75
  • 博客积分: 1216
  • 博客等级: 少尉
  • 技术积分: 1998
  • 用 户 组: 普通用户
  • 注册时间: 2012-08-11 16:20
个人简介

优秀是一种习惯

文章分类

全部博文(75)

文章存档

2014年(1)

2013年(29)

2012年(45)

分类: Python/Ruby

2012-12-30 12:11:04

string模块中的很多函数与python的字符串属性函数类似,就不介绍了。

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)创建一个s到r的转换表,然后可以使用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)
  7. '123xyz789'


阅读(12800) | 评论(0) | 转发(0) |
0

上一篇:sed练习(一)

下一篇:python super()

给主人留下些什么吧!~~