python standard library based on python 3.7.3 https://docs.python.org/3/library/
python标准库 - 文本处理服务 - string库
source code: lib/string.py
link: https://docs.python.org/3/library/string.html#module-string
github code : string constants.py
string.ascii_letters 大小写字母常数
# ascii_letters 大小写字母常数
print(string.ascii_letters) # abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz
string.ascii_lowercase 小写字母常数
# ascii_lowercase 小写字母常数
print(string.ascii_lowercase) # abcdefghijklmnopqrstuvwxyz
string.ascii_uppercase 大写字母常数
# ascii_uppercase 大写字母常数
print(string.ascii_uppercase) # abcdefghijklmnopqrstuvwxyz
string.digits 十进制数字常数
# digits 十进制数字常数
print(string.digits) # 0123456789
string.hexdigits 十六进制数字常数
# hexdigits 十六进制数字常数
print(string.hexdigits) # 0123456789abcdefabcdef
string.octdigits 八进制数字常数
# octdigits 八进制数字常数
print(string.octdigits) # 01234567
string.punctuation ascii字符串,在c语言环境中被视为标点字符
# punctuation ascii字符串,在c语言环境中被视为标点字符
print(string.punctuation) # !"#$%&'()* ,-./:;<=>?@[\]^_`{|}~
string.printable 能够被打印的ascii字符串
# printable 能够被打印的ascii字符串
print(string.printable)
# 0123456789abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz!"#$%&'()* ,-./:;<=>?@[\]^_`{|}~ 还要加上 字符空间,制表符,换行符,返回页面,换页符和垂直选项卡
string.whitespace 字符空间,制表符,换行符,返回页面,换页符和垂直选项卡
# whitespace 包含所有被视为空格的ascii字符的字符串
print(string.whitespace) # 字符空间,制表符,换行符,返回页面,换页符和垂直选项卡
自定义字符串格式 custom string formatting
github code : custom string formatting.py
介绍自定义字符串格式 class string.formatter中主要的3个函数
format(format_string, *args, **kwargs)、vformat(format_string, args, kwargs)、parse(format_string)
format(format_string, *args, **kwargs)
主要的api方法。它采用格式字符串和一组任意位置和关键字参数。它只是一个调用vformat()的包装器。
'''
@description: 主要的api方法。它采用格式字符串和一组任意位置和关键字参数。它只是一个调用vformat()的包装器。
@param:
format_string: 需要去格式化的目标字符串
*args: 任意位置 元组
**kwargs: 关键字参数 字典
@return:
'''
# string.formatter.format(format_string, *args, **kwargs)
data = ("pi = ",3.1415926)
strtmp = "this is a test:{}{:.4f}"
formatter = string.formatter()
strtmp = formatter.format(strtmp,*data) # 元组
print(strtmp) # this is a test:pi = 3.1416
data = {"key1":3.1415926,"key2":"pi = "}
strtmp = "this is a test:{key2}{key1}"
formatter = string.formatter()
strtmp = formatter.format(strtmp,**data) # 字典
print(strtmp) # this is a test:pi = 3.1415926
# string.formatter.format(format_string, *args, **kwargs) end
vformat(format_string, args, kwargs)
此函数执行格式化的实际工作
'''
@description: 此函数执行格式化的实际工作
@param:
format_string: 需要去格式化的目标字符串
args: 任意位置 元组
kwargs: 关键字参数 字典
@return:
'''
# string.formatter.vformat(format_string, args, kwargs)
# 注意 formatter.vformat的参数不是 (*args, **kwargs) 而是 (args, kwargs)
data = ("pi = ",3.1415926)
strtmp = "this is a test:{}{:.4f}"
formatter = string.formatter()
strtmp = formatter.vformat(strtmp,data,{}) # 元组
print(strtmp) # this is a test:pi = 3.1416
data = {"key1":3.1415926,"key2":"pi = "}
strtmp = "this is a test:{key2}{key1}"
formatter = string.formatter()
strtmp = formatter.vformat(strtmp,(),data) # 字典
print(strtmp) # this is a test:pi = 3.1415926
# string.formatter.vformat(format_string, args, kwargs) end
parse(format_string)
循环遍历format_string并返回一个可迭代的元组(literal_text,field_name,format_spec,conversion)。
'''
@description: 循环遍历format_string并返回一个可迭代的元组(literal_text,field_name,format_spec,conversion)。 vformat()使用它将字符串分解为文字文本或替换字段。
@param:
format_string:需要去格式化的目标字符串
@return:
tuples 元组
'''
# string.formatter.parse(format_string)
strtmp = "this is a test:{}{:.4f}"
formatter = string.formatter()
strtuple = formatter.parse(strtmp)
for i, v in enumerate(strtuple):
print(i, v)
'''
0 ('this is a test:', '', '', none)
1 ('', '', '.4f', none)
'''
strtmp = "this is a test:{key2}{key1}"
formatter = string.formatter()
strtuple = formatter.parse(strtmp)
for i, v in enumerate(strtuple):
print(i, v)
'''
0 ('this is a test:', 'key2', '', none)
1 ('', 'key1', '', none)
'''
# string.formatter.parse(format_string) end
github code : format string syntax.py
包含str.format()语法的示例以及与旧的%-formatting的比较。
在大多数情况下,语法类似于旧的%-formatting,添加了{}和with:而不是%。例如,'%03.2f'可以翻译为'{:03.2f}'。
accessing arguments by position: 按位置访问参数:
tupdata = ("this","is","a","test") # 元组
formatstr = '{0} {1} {2} {3}'.format("this","is","a","test")
print(formatstr) # this is a test
formatstr = '{} {} {} {}'.format(*tupdata) # *data 解包参数序列
print(formatstr) # this is a test
formatstr = '{3} {2} {1} {0}'.format(*tupdata) # *data 解包参数序列
print(formatstr) # test a is this
formatstr = '{2} {3} {1} {2} {3}'.format(*tupdata) # 参数可以重复
print(formatstr) # a test is a test
accessing arguments by name:按关键字访问参数:
dicdata = {'author':'leacoder','time':'2019/04/17'}
formatstr = 'the author is {author},the time is {time}'.format(author='leacoder',time='2019/04/17')
print(formatstr) # the author is leacoder,the time is 2019/04/17
formatstr = 'the author is {author},the time is {time}'.format(**dicdata)
print(formatstr) # the author is leacoder,the time is 2019/04/17
accessing arguments’ attributes: 访问参数的属性:
class point:
def __init__(self,x,y):
self.x ,self.y = x, y
point = point(4,2)
formatstr = 'thie point is ({key.x},{key.y})'.format(key = point) # key 可为其他
print(formatstr) # thie point is (4,2)
formatstr = 'thie point is ({point.x},{point.y})'.format(point = point) # point 可为其他
print(formatstr) # thie point is (4,2)
accessing arguments’ items: 访问参数的各项:
tupdata = ("leacoder","2019/04/17") # 元组
formatstr = 'the author is {0[0]},the time is {0[1]}'.format(tupdata)
print(formatstr) # the author is leacoder,the time is 2019/04/17
formatstr = 'the author is {0[0]},the time is {0[1]}'.format(*tupdata) # 注意区别
print(formatstr) # the author is l,the time is e
replacing %s and %r: 替换%s和%r: 使用 !s !r
formatstr = "repr() shows quotes: {!r}; str() doesn't: {!s}".format('test1', 'test2')
print(formatstr) # repr() shows quotes: 'test1'; str() doesn't: test2
aligning the text and specifying a width: 对齐文本并指定宽度:
formatstr = '{:<30}'.format('left aligned') # 左对齐 30位
print(formatstr) # ‘left aligned ’ 为了体现位数加了‘’
formatstr = '{:>30}'.format('right aligned') # 右对齐 30位
print(formatstr) # ‘ right aligned’
formatstr = '{:^30}'.format('centered') # 中间对齐 30位
print(formatstr) # ‘ centered ’
formatstr = '{:*^30}'.format('centered') # 使用* 作为填充字符
print(formatstr) # ‘***********centered***********’
replacing % f, %-f, and % f and specifying a sign: 替换% f,% - f和%f并指定符号:
formatstr = '{: f}; {: f}'.format(3.14, -3.14) # 总是显示它符号
print(formatstr) # ‘ 3.140000; -3.140000’
formatstr = '{: f}; {: f}'.format(3.14, -3.14) # 正数前显示空格
print(formatstr) # ‘ 3.140000; -3.140000’
formatstr = '{:-f}; {:-f}'.format(3.14, -3.14) # 只显示负号 同 '{:f}; {:f}'
print(formatstr) # ‘3.140000; -3.140000’
replacing %x and %o and converting the value to different bases: 替换%x和%o并将值转换为不同的进制:
formatstr = "int: {0:d}; hex: {0:x}; oct: {0:o}; bin: {0:b}".format(64)
print(formatstr) # int: 64; hex: 40; oct: 100; bin: 1000000
formatstr = "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(64)
print(formatstr) # int: 64; hex: 0x40; oct: 0o100; bin: 0b1000000
formatstr = "int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}".format(0b1000001) # 也支持其他进制
print(formatstr) # int: 65; hex: 0x41; oct: 0o101; bin: 0b1000001
using the comma as a thousands separator: 使用逗号作为千位分隔符:
formatstr = '{:,}'.format(1234567890)
print(formatstr) # 1,234,567,890
expressing a percentage: 表达百分比:
points = 1
total = 3
formatstr = 'points / total = {:.2%}'.format(points/total)
print(formatstr) # points / total = 33.33%
using type-specific formatting: 使用特定类型的格式:
import datetime
d = datetime.datetime(2019, 4, 17, 22, 49, 2) # 2019/04/17 22:49:02
formatstr = '{:%y-%m-%d %h:%m:%s}'.format(d)
print(formatstr) # 2019-04-17 22:49:02
github code : template strings.py
模板字符串的规则
'''
模板字符串提供更简单的字符串替换,如pep 292中所述 https://www.python.org/dev/peps/pep-0292/
模板字符串支持基于$的替换,使用以下规则:
1、$$是转义; 它被替换为单个$。
2、$identifier 一个替换占位符,用于匹配映射关键字“identifier”默认情况下,
“标识符”仅限于以下划线或ascii字母开头的任何不区分大小写的ascii字母数字字符串(包括下划线)。$字符后面的第一个非标识符字符结束此占位符。
3、$ {identifier}相当于$ identifier。当有效标识符字符跟随占位符但不是占位符的一部分时,例如“$ {noun} ification”,则需要它。
4、字符串中$的任何其他形式都将导致引发valueerror。
字符串模块提供实现这些规则的template类。class string.template(template)
'''
class string.template 类
substitute(mapping, **kwds)
'''
@description: 执行模板替换,返回一个新字符串。
@param:
mapping:任何类似字典的对象,其键与模板中的占位符匹配。
**kewds: 关键字参数,其中关键字是占位符。
当给出mapping和kwds并且存在重复时,来自kwds的占位符优先。
@return: 返回一个新字符串
'''
# substitute(mapping, **kwds)
s = template('the author is $author, the time is $time') # 使用template类构造函数
kewds = {'author':'leacoder', 'time':'2019/04/18 00:01:38'}
templatestr = s.substitute(author='leacoder', time='2019/04/18 00:01:38') # **kewds
print(templatestr) # the author is leacoder, the time is 2019/04/18 00:01:38
templatestr = s.substitute(**kewds) # **kewds
print(templatestr) # the author is leacoder, the time is 2019/04/18 00:01:38
templatestr = s.substitute(kewds) # mapping
print(templatestr) # the author is leacoder, the time is 2019/04/18 00:01:38
templatestr = s.substitute(kewds,author='250',time = 'no time') # mapping **kewds
print(templatestr) # the author is 250, the time is no time
kewds1 = {'author':'leacoder'}
templatestr = s.substitute(kewds1)
print(templatestr) # keyerror: 'time'
# substitute(mapping, **kwds) end
safe_substitute(mapping, **kwds)
'''
@description:
与substitute()一样,如果map和kwds中缺少占位符,原始占位符将在结果字符串中完整显示,而不是引发keyerror异常
此外,与substitute()不同,$的任何其他形式只会返回$而不是引发valueerror。
@param:
同 substitute()
@return:
'''
# safe_substitute(mapping, **kwds)
kewds1 = {'author':'leacoder'}
templatestr = s.safe_substitute(kewds1)
print(templatestr) # the author is leacoder, the time is $time
# safe_substitute(mapping, **kwds) end