Python 模塊學習:string模塊1
阿新 • • 發佈:2018-08-17
emp white ati att () ase nta true tdi
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,後續研究。
Python 模塊學習:string模塊1