string模块

Posted shucomputerprogram

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了string模块相关的知识,希望对你有一定的参考价值。

# string Moudle

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

x0bx0c‘
"""

 

# 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))

 


以上是关于string模块的主要内容,如果未能解决你的问题,请参考以下文章

CSP核心代码片段记录

CTS测试CtsWindowManagerDeviceTestCases模块的testShowWhenLockedImeActivityAndShowSoftInput测试fail项解决方法(代码片段

如何将代码片段存储在 mongodb 中?

如何将字符串数据从活动发送到片段?

如何理解这段代码片段中的两对括号?

Failed to convert property value of type ‘java.lang.String‘ to required type ‘int‘ for property(代码片段