【发布时间】:2016-07-12 10:07:00
【问题描述】:
我正在尝试教我的朋友 Python,并向他展示了 3 种打印不同类型变量的方法,而无需先将它们转换为字符串,但他想知道应该使用哪个,我不知道好久没用Python了,希望大家帮帮忙:
print("There are %d people in total" % num_people)
或
print("There are", num_people, "people in total")
或
print("There are {} people in total".format(num_people))
我知道 format 方法可以执行其他更强大的字符串操作操作,但除此之外,我不确定哪种方法是首选方法。我不知道为什么第一种方法存在,因为第二种方法似乎更简单。
对此有什么想法吗?谢谢!
【问题讨论】:
标签: python string variables printing