Chinaunix首页 | 论坛 | 博客
  • 博客访问: 483239
  • 博文数量: 28
  • 博客积分: 858
  • 博客等级: 一等列兵
  • 技术积分: 852
  • 用 户 组: 普通用户
  • 注册时间: 2011-06-02 14:13
文章分类

全部博文(28)

文章存档

2020年(1)

2019年(1)

2018年(5)

2017年(1)

2014年(1)

2013年(2)

2012年(17)

分类: LINUX

2013-08-13 15:27:55

模块string

模块string有很多常量和函数,它的大部分函数已经移植到str类型中去了,很多模块常量还是非常有用的,比如:

点击(此处)折叠或打开

  1. In [19]: import string

  2. In [20]: print string.letters
  3. abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ
抛出一个问题,比如我们需要一个字母集,可以通过获取这个常量获取。string主要的常量有这些:

点击(此处)折叠或打开

  1. whitespace -- a string containing all characters considered whitespace
  2. lowercase -- a string containing all characters considered lowercase letters
  3. uppercase -- a string containing all characters considered uppercase letters
  4. letters -- a string containing all characters considered letters
  5. digits -- a string containing all characters considered decimal digits
  6. hexdigits -- a string containing all characters considered hexadecimal digits
  7. octdigits -- a string containing all characters considered octal digits
  8. punctuation -- a string containing all characters considered punctuation
  9. printable -- a string containing all characters considered printable
目前保留的函数:
capwords, 作用是把一个字符串中的单词第一个字母大写。
maketrans, 生成一个字符串到字符串的映射,可以使用translate,把str中的映射源字符被目的字符替换。
字符串模板:
string.Template
创建模板 t=string.Template(str)
使用模板 t.substitute(values)
阅读(3851) | 评论(0) | 转发(0) |
0

上一篇:linux 命令大杂烩

下一篇:python日志解读

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