string 模块

string 模块简单分析

字符串常量

点击查看代码
whitespace = ' \t\n\r\v\f'  # 空白字符
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'  # 全英文小写
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'  # 全英文大些
ascii_letters = ascii_lowercase + ascii_uppercase  # asscii码 字母
digits = '0123456789'  # 十进制数字
hexdigits = digits + 'abcdef' + 'ABCDEF'  # 十六进制
octdigits = '01234567'  # 八进制
punctuation = r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""  # 特殊字符
printable = digits + ascii_letters + punctuation + whitespace  # 可打印的

格式化字符串

点击查看代码
import string


# 千分位显示
print('{:,}'.format(1234567890))  # '1,234,567,890'
# 百分比表示
print('Correct answers: {:.2%}'.format(23 / 16))  # 'Correct answers: 143.75%'
# 指定小数点后几位展示
print('{:.2f}; {:.5f}'.format(3.1415926, -3.1415926))  # '3.14; -3.14159'

# 格式化字符串
print('{0}, {1}, {2}'.format('a', 'b', 'c'))  # a, b, c
print('{}, {}, {}'.format('a', 'b', 'c'))  # a, b, c
print('{2}, {1}, {0}'.format('a', 'b', 'c'))  # c, b, a
print('{2}, {1}, {0}'.format(*'abc'))  # c, b, a     # 解包参数序列
print('{0}{1}{0}'.format('AA', 'BB'))  # AABBAA      # 参数的下标可以重复
print('姓名:{name},年龄 {age}'.format(**{'name': 'jwd', 'age': 18}))  # 姓名:jwd,年龄 18
print('姓名:{name},年龄 {age},姓名:{name}'.format(name='jwd', age=18))  # 姓名:jwd,年龄 18,姓名:jwd
coord = (3, 5)
print('X: {0[0]};  Y: {0[1]}'.format(coord))  # 'X: 3;  Y: 5'
#  格式规范小型语言
print('{:+f}; {:+f}'.format(3.14, -3.14))  # '+3.140000; -3.140000'
print('{: f}; {: f}'.format(3.14, -3.14))  # ' 3.140000; -3.140000'
print('{:-f}; {:-f}'.format(3.14, -3.14))  # '3.140000; -3.140000'
#  格式字符串语法
print("名字是 {0!s}".format('JingWenDong'))  # 相当于使用了 str()
print("名字是 {name!r}".format(name='JingWenDong'))  # 相当于使用了 repr()
print("名字是 {!a}".format('JingWenDong'))  # 相当于使用了 ascii()
#  ‘#’的作用
print("int: {0:d};  hex: {0:x};  oct: {0:o};  bin: {0:b}".format(42))  # int: 42;  hex: 2a;  oct: 52;  bin: 101010
print("int: {0:d};  hex: {0:#x};  oct: {0:#o};  bin: {0:#b}".format(42))  # int: 42;  hex: 0x2a;  oct: 0o52;  bin: 0b101010
# 对象类展示
class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y

    def __str__(self):
        return 'Point({self.x}, {self.y})'.format(self=self)
print(Point(3, 5))  # Point(3, 5)
#  不设置宽度表示
print('十进制={0:d},八进制={0:o},十六进制={0:X},二进制={0:b},'.format(12))
#  设置宽度表示
for num in range(5, 12):
    for base in 'dXob':
        print('{0:{width}{base}}'.format(num, base=base, width=1), end=' ')
    print()

模板字符串

点击查看代码
"""
前景提要
1)substitute()  参数可以是 任何map类型的数据,或者关键字参数
2)substitute()  参数出出现重复项,则优先考虑 关键字参数结果 
"""
# 创建模板对象
s = Template('$who likes $what')

# 替换方法 (字符串中没有关键字对象则引发异常)
print(s.substitute({'who': 'jwd', 'what': 'goods'}))  # jwd likes goods

# 重复项 (参数出出现重复项,则优先考虑 关键字参数结果)
print(s.substitute({'who': 'jwd', 'what': 'goods'}, what='coke'))  # jwd likes coke

# 安全替换方法 (字符串中没有关键字对象不引发异常,直接显示未匹配的结果)
print(s.safe_substitute({'who': 'jwd'}))  # jwd likes $what



""" 高级使用 """

# 可以派生 Template 自定义用于分析模板字符串的占位符语法、分隔符或整个正则表达式。为此,可以重写这些类属性:delimiter,idpattern

class NewTemplate(Template):
    delimiter = '@'
    idpattern = r'(?a:[_a-z][_a-z0-9]*)'


# 新对象测试
s = NewTemplate('@who likes @what')
print(s.substitute({'who': 'jwd', 'what': 'goods'}))  # jwd likes goods
print(s.substitute({'who': 'jwd', 'what': 'goods'}, what='coke'))  # jwd likes coke
print(s.safe_substitute({'who': 'jwd'}))  # jwd likes $what
posted @ 2022-12-28 11:10  空白的时间  阅读(50)  评论(0编辑  收藏  举报