Python 模块学习:string模块1
Posted luoheng23
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Python 模块学习:string模块1相关的知识,希望对你有一定的参考价值。
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 = ‘ vf‘ ascii_lowercase = ‘abcdefghijklmnopqrstuvwxyz‘ ascii_uppercase = ‘ABCDEFGHIJKLMNOPQRSTUVWXYZ‘ ascii_letters = ascii_lowercase + ascii_uppercase digits = ‘0123456789‘ hexdigits = digits + ‘abcdef‘ + ‘ABCDEF‘ octdigits = ‘01234567‘ punctuation = r"""!"#$%&‘()*+,-./:;<=>[email protected][]^_`{|}~""" 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,后续研究。