Python 格式化输出 —— format()
的使用
(1)不使用序号
day = "三"
weather = "晴朗"
test = "今天是星期{},天气{}".format(day, weather)
print(test)
>>> 今天是星期三,天气晴朗
(2)使用序号
name = "张三"
age = 21
thing = "去上学"
res = "姓名:{0},年龄:{1},{0}今天{2}了".format(name,age,thing)
print(res)
>>>姓名:张三,年龄:21,张三今天去上学了
(3)变量赋值
name = "张三"
age = 21
thing = "去上学"
res = "姓名:{zz},年龄:{xx},{zz}今天{cc}了".format(zz=name,xx=age,cc=thing)
print(res)
>>>姓名:张三,年龄:21,张三今天去上学了