文档:https://docs.python.org/zh-cn/3.9/library/string.html#template-strings
代码示例
from string import Template s = Template('I am ${name}') ret = s.substitute(name="Tom") print(ret) # I am Tom
继承使用,自定义占位符
from string import Template class MyTemplate(Template): delimiter = "" # 起始分隔符 idpattern = "" # 不带花括号的占位符的模式的正则表达式 braceidpattern = r"\w+" # 带花括号的占位符的模式 data = { "uid": '1', "name": "Tom" } s = MyTemplate('/student/{uid}/{name}') ret = s.substitute(**data) print(ret) # /student/1/Tom