string 模块提供了许多字符串常量, 如下所示:
- __all__ = ["ascii_letters", "ascii_lowercase", "ascii_uppercase", "capwords",
- "digits", "hexdigits", "octdigits", "printable", "punctuation",
- "whitespace", "Formatter", "Template"]
- # Some strings for ctype-style character classification
- whitespace = '\t\n\r\v\f'
- ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
- ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
- ascii_letters = ascii_lowercase + ascii_uppercase
- digits = '0123456789'
- hexdigits = digits + 'abcdef' + 'ABCDEF'
- octdigits = '01234567'
- punctuation = r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
- printable = digits + ascii_letters + punctuation + whitespace
这些常量在很多场合很有用处, 比如要去掉字符串左边的所有字母:
- >>> import string
- >>> a = "asdfjsdnfnfjirrjwoe12345dsf6sdfjksdfj"
- >>> a.lstrip(string.ascii_letters)
- '12345dsf6sdfjksdfj'
- >>>
提供了一个函数 capwords, 函数原型为:
- def capwords(s, sep=None):
- return (sep or ' ').join(x.capitalize() for x in s.split(sep))
还有两个类: Template 和 Formatter, 后续研究.
来源: http://www.bubuko.com/infodetail-2730644.html