如何在 Python 的同一行上打印变量和字符串?
Posted
技术标签:
【中文标题】如何在 Python 的同一行上打印变量和字符串?【英文标题】:How can I print variable and string on same line in Python? 【发布时间】:2013-06-13 18:38:01 【问题描述】:我正在使用 python 计算如果每 7 秒有一个孩子出生,那么 5 年内将出生多少个孩子。问题出在我的最后一行。当我在它的任一侧打印文本时,如何让变量工作?
这是我的代码:
currentPop = 312032486
oneYear = 365
hours = 24
minutes = 60
seconds = 60
# seconds in a single day
secondsInDay = hours * minutes * seconds
# seconds in a year
secondsInYear = secondsInDay * oneYear
fiveYears = secondsInYear * 5
#Seconds in 5 years
print fiveYears
# fiveYears in seconds, divided by 7 seconds
births = fiveYears // 7
print "If there was a birth every 7 seconds, there would be: " births "births"
【问题讨论】:
2020 年要小心(常识,我知道 :D)。打印在 Python3 中已经成为一个函数,现在需要用括号来使用:print(something)
(另外 Python2 从今年开始就已经过时了。)
【参考方案1】:
还有两个
第一个
>>> births = str(5)
>>> print("there are " + births + " births.")
there are 5 births.
添加字符串时,它们会连接起来。
第二个
字符串的format
(Python 2.6 和更新版本)方法也可能是标准方法:
>>> births = str(5)
>>>
>>> print("there are births.".format(births))
there are 5 births.
format
方法也可以与列表一起使用
>>> format_list = ['five', 'three']
>>> # * unpacks the list:
>>> print("there are births and deaths".format(*format_list))
there are five births and three deaths
或字典
>>> format_dictionary = 'births': 'five', 'deaths': 'three'
>>> # ** unpacks the dictionary
>>> print("there are births births, and deaths deaths".format(**format_dictionary))
there are five births, and three deaths
【讨论】:
【参考方案2】:Python 是一种用途广泛的语言。您可以通过不同的方法打印变量。我在下面列出了五种方法。您可以根据自己的方便使用它们。
示例:
a = 1
b = 'ball'
方法一:
print('I have %d %s' % (a, b))
方法二:
print('I have', a, b)
方法三:
print('I have '.format(a, b))
方法四:
print('I have ' + str(a) + ' ' + b)
方法五:
print(f'I have a b')
输出将是:
I have 1 ball
【讨论】:
决定和你的编程风格有关:M2是过程式编程,M3是面向对象编程。 M5 的关键字是formatted string literal。如果需要,应该使用像 M1 和 M4 这样的字符串操作,这里不是这种情况(M1 用于字典和元组;M4 例如用于 ascii-art 和其他格式化输出)【参考方案3】:打印时使用,
分隔字符串和变量:
print("If there was a birth every 7 seconds, there would be: ", births, "births")
打印函数中的,
用一个空格分隔项目:
>>> print("foo", "bar", "spam")
foo bar spam
或者更好地使用string formatting:
print("If there was a birth every 7 seconds, there would be: births".format(births))
字符串格式化功能更强大,还允许您执行一些其他操作,例如填充、填充、对齐、宽度、设置精度等。
>>> print(":d :03d :>20f".format(1, 2, 1.1))
1 002 1.100000
^^^
0's padded to 2
演示:
>>> births = 4
>>> print("If there was a birth every 7 seconds, there would be: ", births, "births")
If there was a birth every 7 seconds, there would be: 4 births
# formatting
>>> print("If there was a birth every 7 seconds, there would be: births".format(births))
If there was a birth every 7 seconds, there would be: 4 births
【讨论】:
【参考方案4】:您可以使用 f-string 或 .format() 方法
使用 f-string
print(f'If there was a birth every 7 seconds, there would be: births births')
使用 .format()
print("If there was a birth every 7 seconds, there would be: births births".format(births=births))
【讨论】:
【参考方案5】:如果你在字符串和变量之间使用逗号,像这样:
print "If there was a birth every 7 seconds, there would be: ", births, "births"
【讨论】:
【参考方案6】:只需在中间使用 ,(逗号)。
请参阅此代码以更好地理解:
# Weight converter pounds to kg
weight_lbs = input("Enter your weight in pounds: ")
weight_kg = 0.45 * int(weight_lbs)
print("You are ", weight_kg, " kg")
【讨论】:
【参考方案7】:Python 3
最好使用格式选项
user_name=input("Enter your name : )
points = 10
print ("Hello, your point is : ".format(user_name,points)
或将输入声明为字符串并使用
user_name=str(input("Enter your name : ))
points = 10
print("Hello, "+user_name+" your point is " +str(points))
【讨论】:
String"Enter your name :
缺少右引号
print ("Hello, your point is : ".format(user_name,points)
缺少右括号。【参考方案8】:
如果您使用的是 python 3.6 或最新版本, f-string 是最好的和最简单的
print(f"your_varaible_name")
【讨论】:
【参考方案9】:略有不同:使用 Python 3 并在同一行中打印 几个 变量:
print("~~Create new DB:",argv[5],"; with user:",argv[3],"; and Password:",argv[4]," ~~")
【讨论】:
【参考方案10】:从 python 3.6 开始,您可以使用 Literal String Interpolation.
births = 5.25487
>>> print(f'If there was a birth every 7 seconds, there would be: births:.2f births')
If there was a birth every 7 seconds, there would be: 5.25 births
【讨论】:
【参考方案11】:使用String formatting
print("If there was a birth every 7 seconds, there would be: births".format(births))
# Will replace "" with births
如果你做一个玩具项目使用:
print('If there was a birth every 7 seconds, there would be:' births'births)
或
print('If there was a birth every 7 seconds, there would be: %d births' %(births))
# Will replace %d with births
【讨论】:
【参考方案12】:在当前的 python 版本中,您必须使用括号,如下所示:
print ("If there was a birth every 7 seconds", X)
【讨论】:
【参考方案13】:您将首先创建一个变量:例如:D = 1。然后执行此操作,但将字符串替换为您想要的任何内容:
D = 1
print("Here is a number!:",D)
【讨论】:
【参考方案14】:如果你想使用 python 3,这很简单:
print("If there was a birth every 7 second, there would be %d births." % (births))
【讨论】:
【参考方案15】:我将您的脚本复制并粘贴到 .py 文件中。我使用 Python 2.7.10 按原样运行它并收到相同的语法错误。我还尝试了 Python 3.5 中的脚本并收到以下输出:
File "print_strings_on_same_line.py", line 16
print fiveYears
^
SyntaxError: Missing parentheses in call to 'print'
然后,我修改了打印出生人数的最后一行,如下所示:
currentPop = 312032486
oneYear = 365
hours = 24
minutes = 60
seconds = 60
# seconds in a single day
secondsInDay = hours * minutes * seconds
# seconds in a year
secondsInYear = secondsInDay * oneYear
fiveYears = secondsInYear * 5
#Seconds in 5 years
print fiveYears
# fiveYears in seconds, divided by 7 seconds
births = fiveYears // 7
print "If there was a birth every 7 seconds, there would be: " + str(births) + " births"
输出是(Python 2.7.10):
157680000
If there was a birth every 7 seconds, there would be: 22525714 births
我希望这会有所帮助。
【讨论】:
【参考方案16】:您可以使用格式字符串:
print "There are %d births" % (births,)
或者在这个简单的例子中:
print "There are ", births, "births"
【讨论】:
如果使用第二种方式要小心,因为那是一个元组,而不是一个字符串。【参考方案17】:您可以使用string formatting 来执行此操作:
print "If there was a birth every 7 seconds, there would be: %d births" % births
或者你可以给print
多个参数,它会自动用空格分隔它们:
print "If there was a birth every 7 seconds, there would be:", births, "births"
【讨论】:
感谢 Amber 的回答。你能解释一下 % 符号后面的 'd' 做什么吗?谢谢%d
表示“将值格式化为整数”。同样,%s
是“将值格式化为字符串”,%f
是“将值格式化为浮点数”。我在答案中链接到的 Python 手册部分记录了这些以及更多内容。以上是关于如何在 Python 的同一行上打印变量和字符串?的主要内容,如果未能解决你的问题,请参考以下文章
如何使通过公共相关 ID 链接的变量显示在 Splunk 表的同一行上?