码迷,mamicode.com
首页 > 编程语言 > 详细

Python 模块学习:string模块1

时间:2018-08-16 22:26:42      阅读:182      评论:0      收藏:0      [点我收藏+]

标签:学习   ssi   nta   原型   cas   asc   ace   mat   spl   

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

标签:学习   ssi   nta   原型   cas   asc   ace   mat   spl   

原文地址:https://www.cnblogs.com/luoheng23/p/9490281.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!