Python字符串拼接变量的优雅写法
方法:
在python中 字符串前加 f表示:在字符串内支持大括号内的python表达式
import time
name = "tom"
# 常规写法
print("his name is " + name + "!")
print("present time is "+ str(int(time.time())))
# 分隔符
print('&'*50)
# 优雅效率写法
print(f"his name is {name}!")
print(f"present time is {int(time.time())}")
输出结果:
his name is tom!
present time is 1650351984
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&
his name is tom!
present time is 1650351984