如何在 Python 的同一行上打印变量和字符串?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17153779/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How can I print variable and string on same line in Python?
提问by Bob Uni
I am using python to work out how many children would be born in 5 years if a child was born every 7 seconds. The problem is on my last line. How do I get a variable to work when I'm printing text either side of it?
我正在使用 python 计算如果每 7 秒就有一个孩子出生,那么 5 年内会出生多少个孩子。问题出在我的最后一行。当我在变量的任一侧打印文本时,如何让变量起作用?
Here is my code:
这是我的代码:
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"
采纳答案by Ashwini Chaudhary
Use ,to separate strings and variables while printing:
用于,在打印时分隔字符串和变量:
print "If there was a birth every 7 seconds, there would be: ",births,"births"
,in print statement separtes the items by a single space:
,在打印语句中用一个空格分隔项目:
>>> print "foo","bar","spam"
foo bar spam
or better use string formatting:
或者更好地使用字符串格式:
print "If there was a birth every 7 seconds, there would be: {} births".format(births)
String formatting is much more powerful and allows you to do some other things as well, like : padding, fill, alignment,width, set precision etc
字符串格式更强大,还允许你做一些其他的事情,比如:填充、填充、对齐、宽度、设置精度等
>>> print "{:d} {:03d} {:>20f}".format(1,2,1.1)
1 002 1.100000
^^^
0's padded to 2
Demo:
演示:
>>> 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
回答by Amber
You can use string formattingto do this:
您可以使用字符串格式来执行此操作:
print "If there was a birth every 7 seconds, there would be: %d births" % births
or you can give printmultiple arguments, and it will automatically separate them by a space:
或者你可以给出print多个参数,它会自动用空格分隔它们:
print "If there was a birth every 7 seconds, there would be:", births, "births"
回答by enpenax
You can either use a formatstring:
您可以使用格式字符串:
print "There are %d births" % (births,)
or in this simple case:
或者在这个简单的情况下:
print "There are ", births, "births"
回答by TehTris
two more
还有两个
The First one
第一个
>>>births = str(5)
>>>print "there are " + births + " births."
there are 5 births.
When adding strings, they concatenate.
添加字符串时,它们会连接起来。
The Second One
第二个
Also the format(Python 2.6 and newer) method of strings is probably the standard way:
此外format,字符串的(Python 2.6 和更新版本)方法可能是标准方法:
>>> births = str(5)
>>>
>>> print "there are {} births.".format(births)
there are 5 births.
This formatmethod can be used with lists as well
此format方法也可用于列表
>>> format_list = ['five','three']
>>> print "there are {} births and {} deaths".format(*format_list) #unpack the list
there are five births and three deaths
or dictionaries
或字典
>>> format_dictionary = {'births': 'five', 'deaths': 'three'}
>>> print "there are {births} births, and {deaths} deaths".format(**format_dictionary) #yup, unpack the dictionary
there are five births, and three deaths
回答by Debug255
I copied and pasted your script into a .py file. I ran it as-is with Python 2.7.10 and received the same syntax error. I also tried the script in Python 3.5 and received the following output:
我将您的脚本复制并粘贴到 .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'
Then, I modified the last line where it prints the number of births as follows:
然后,我修改了打印出生人数的最后一行,如下所示:
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"
The output was (Python 2.7.10):
输出是(Python 2.7.10):
157680000
If there was a birth every 7 seconds, there would be: 22525714 births
I hope this helps.
我希望这有帮助。
回答by Dror
On a current python version you have to use parenthesis, like so :
在当前的 python 版本上,您必须使用括号,如下所示:
print ("If there was a birth every 7 seconds", X)
回答by Python Coding Trainer
If you want to work with python 3, it's very simple:
如果你想使用 python 3,这很简单:
print("If there was a birth every 7 second, there would be %d births." % (births))
回答by Pythonbites Programming
You would first make a variable: for example: D = 1. Then Do This but replace the string with whatever you want:
您将首先创建一个变量:例如:D = 1。然后执行此操作,但将字符串替换为您想要的任何内容:
D = 1
print("Here is a number!:",D)
回答by Gagan Agrawal
Python is a very versatile language. You may print variables by different methods. I have listed below 4 methods. You may use them according to your convenience.
Python 是一种非常通用的语言。您可以通过不同的方法打印变量。我在下面列出了4种方法。您可以根据自己的方便使用它们。
Example:
例子:
a=1
b='ball'
Method 1:
方法一:
print('I have %d %s' %(a,b))
Method 2:
方法二:
print('I have',a,b)
Method 3:
方法三:
print('I have {} {}'.format(a,b))
Method 4:
方法四:
print('I have ' + str(a) +' ' +b)
Method 5:
方法五:
print( f'I have {a} {b}')
Output would be:
输出将是:
I have 1 ball
回答by Siddharth Dash
使用字符串格式
print("If there was a birth every 7 seconds, there would be: {} births".format(births))
# Will replace "{}" with births
if you doing a toy project use:
如果你做一个玩具项目使用:
print('If there was a birth every 7 seconds, there would be:' births'births)
or
或者
print('If there was a birth every 7 seconds, there would be: %d births' %(births))
# Will replace %d with births

