标签:保留 表达 except template strong ide rpo key VID
import string values = {‘var‘: ‘foo‘} t = string.Template(""" Variable : $var Escape : $$ Variable in text: ${var}iable """) print(‘TEMPLATE: ‘, t.substitute(values))
输出结果:
TEMPLATE: Variable : foo Escape : $ Variable in text: fooiable
2、使用%操作符的类似字符串拼接
s1 = """ Variable : %(var)s Escape : %% Variable in text: %(var)siable """ print(‘TERPOLATION‘, s1 % values)
输出结果:
TERPOLATION Variable : foo Escape : % Variable in text: fooiable
3、使用 str. format ()格式
s2 = """ Variable : {var} Escape : {{}} Variable in text: {var}iable """ print(‘FORMAT: ‘, s2.format(**values))
输出结果:
FORMAT: Variable : foo Escape : {} Variable in text: fooiable
values = {‘var‘: ‘foo‘} t = string.Template("$var is here but $missing is not provided") try: print(‘substitute() :‘, t.substitute(values)) except KeyError as e: print(‘ERROR:‘, str(e)) print(‘safe_substitute(): ‘, t.safe_substitute(values))
输出结果:
ERROR: ‘missing‘ safe_substitute(): foo is here but $missing is not provided
标签:保留 表达 except template strong ide rpo key VID
原文地址:https://www.cnblogs.com/fjy49/p/14425831.html