- 字符串拼接
>>> a, b, c = 1, 2, 3
>>> print(a, b, c, sep=':')
1:2:3
- 字符串替换
>>> s = '{name} has {n} messages.'
>>> s.format(name='Guido', n='37')
'Guido has 37 messages.'
>>> name = 'XiaoMing'
>>> n = 19
>>> s.format_map(vars())
'XiaoMing has 19 messages.'
>>> class Info:
... def __init__(self, name, n):
... self.name = name
... self.n = n
...
>>> a = Info('Guido',37)
>>> s.format_map(vars(a))
'Guido has 37 messages.'
- 如果值并不存在, 如何不报错
参考: https://python3-cookbook.readthedocs.io/zh_CN/latest/c02/p15_interpolating_variables_in_strings.html