string模块

# string Moudle

"""
Data:
    ascii_letters          大小写字母 
    ascii_lowercase        小写字母
    ascii_uppercase        大写字母
    digits                 数字0-9
    hexdigits              十六进制 0123456789abcdefABCDEF
    octdigits              八进制数 01234567  
    printable              全ascii字符
    punctuation            标点符号
    whitespace             '\t\n\r\x0b\x0c'
"""

 

# string方法
imoport string
s
= 'learn python' print(string.capwords(s)) # 转为标题,单词首字母大写

 

import string
d = {'a':'var'}

s ="""
V:${a}
E:$$
T:${a}able
"""

t = string.Template(s)  # $为其语法结构
print(t.substitute(d))
# Templates类的2个方法区别
import string

d = {'a':'key'}
s = '$a is here but $missing is not provided'

t = string.Template(s)

try:
    print('substitute() :', t.substitute(d))
except KeyError as err:
    print('ERROR:', str(err))

print('safe_substitute():', t.safe_substitute(d))
# 进阶
import string

class MyTemplate(string.Template):
    delimiter = '%'  # 语法$修改为语法%
    idpattern = '[a-z]+_[a-z]+'  # 该形式的正则,进行语法,否则忽略


d = {
    'with_underscore': 'replaced',
    'notunderscored': 'not replaced'
}

s = """
D:%%
R:%with_underscore
I:%notunderscored
"""

t = MyTemplate(s)
print(t.safe_substitute(d))

 

转载于:https://www.cnblogs.com/ShuComputerProgram/p/10337669.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值