722 B
722 B
一些变量小技巧
:::tip
https://docs.python.org/3/library/string.html :::
旧式字符串格式化(% 运算符)
print("hello %s, this is %d" %('world',45))
变量替换:
新式字符串格式化(str.format)
print('{2}, {1}, {0}'.format('a', 'b', 'c'))
print("hello {name}, this is {total}".format(name='world',total=45))
字符串插值 / f-Strings(Python 3.6+)
name,total='world',45
print(f"hello {name}, this is {total}" )
模板字符串(标准库)
s = Template('$who likes $what')
print(s.substitute(who='tim', what='kung pao'))
d=dict(who='tim')
Template('$who likes $what').safe_substitute(d)