我只是想知道在打印行中包含变量的最佳方法是什么。
例如,这样做会更好吗:
print("The cost will be $" + str(cost) + ".")
或
print("The cost will be ${}.".format(cost))
谢谢, 大卫
附注在解释时,请注意,我一般来说是编程新手,谢谢:D
最佳答案
我想说,目前,这更好:
print("The cost will be ${}.".format(cost))
因为不需要转换为字符串。
注意:如果你的Python版本超过3.5,请执行print(f'The cost will be${cost}')
,因为它是最好的。
请注意,还有其他几种同样好的方法(适用于所有版本):
print('The cost will be$%s'%cost)
或者(也许不太好..):
print("The cost will be $",cost,".",sep='')
关于python - 在 Python3 中包含变量的最佳方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53111467/