1、术语说明:
· str.format( ) 方法通过字符串中的花括号 { } 来识别需要替换的字段,从而完成字符串的格式化。
省略字段名:
(1)注意:花括号的数量可以少于位置参数,反之则不行!!!
print("大家好,我叫{},来自{}".format('^&*%^&*','火星'))
# 大家好,我叫^&*%^&*,来自火星
2、使用变量名传递关键字参数:
print("大家好,我叫{name},来自{where}".format(name = '^&*%^&*',where = '火星'))
# 大家好,我叫^&*%^&*,来自火星
3、使用元组或字典传参:
t = ('^&*%^&*','火星')
print("大家好,我叫{},来自{}".format(*t))
# 大家好,我叫^&*%^&*,来自火星
d = {'name':'^&*%^&*','where':'火星'}
print("大家好,我叫{name},来自{where}".format(**d))
# 大家好,我叫^&*%^&*,来自火星