一些变量小技巧
小于 1 分钟
一些变量小技巧
旧式字符串格式化(% 运算符)
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)
- r means raw
- b means bytes
- u means unicode
- f means format
rstring会返回原始字符串
dat = r"E:\PycharmProjects\a.py"