1.capwords函数
import string
#capwords 字符串所有单词首字母大写
a = 'i am a goog boy! hello world!'
print(a)
print(string.capwords(a))
output:i am a goog boy! hello world!
I Am A Goog Boy! Hello World!
2.模板调用
这里注意三种方法的特殊性:
import string
values = {'var': 'foo'}
t = string.Template("""
Variable : $var
Escape : $$
Variable in text: ${var}iable
""")
print('TEMPLATE:', t.substitute(values))#substitute用$和${}表示置换
s = """
Variable : %(var)s
Escape : %%
Variable in text: %(var)siable
"""
print('INTERPOLATION:', s % values)#%e用&()s表示置换
s = """
Variable : {var}
Escape : {{}}
Variable in text: {var}iable
"""
print('FORMAT:', s.format(**values))#format函数用{}表示置换
当模板需要的值没有全部作为参数提供给模板时,变量表达式将留在其中
print('safe_substitute():', t.safe_substitute(values))
output:safe_substitute(): foo is here but $missing is not provided