如何在Python中的同一行上打印变量和字符串?

时间:2021-09-01 22:57:14

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?

如果孩子每7秒钟出生一次,我正在使用python来计算出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"

9 个解决方案

#1


128  

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语句中,用单个空格分隔项目:

>>> 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

#2


36  

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:

字符串的格式(Python 2.6和更新)方法也许是标准方法:

>>> births = str(5)
>>>
>>> print "there are {} births.".format(births)
there are 5 births.

This format method can be used with lists as well

此格式方法也可以与列表一起使用

>>> 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

#3


15  

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))

#4


8  

You can either use a formatstring:

您可以使用formatstring:

print "There are %d births" % (births,)

or in this simple case:

或者在这个简单的情况下:

print "There are ", births, "births"

#5


3  

On a current python version you have to use parenthesis, like so :

在当前的python版本中,您必须使用括号,如下所示:

print ("If there was a birth every 7 seconds", X)

#6


2  

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)

#7


1  

You can use string formatting to do this:

您可以使用字符串格式来执行此操作:

print "If there was a birth every 7 seconds, there would be: %d births" % births

or you can give print multiple arguments, and it will automatically separate them by a space:

或者你可以给多个参数打印,它会自动用空格分隔:

print "If there was a birth every 7 seconds, there would be:", births, "births"

#8


1  

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.

我希望这有帮助。

#9


0  

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:

方法1:

print('I have %d %s' %(a,b))

Method 2:

方法2:

print('I have',a,b)

Method 3:

方法3:

print('I have {} {}'.format(a,b))

Method 4:

方法4:

print('I have ' + str(a) +' ' +b)

Output would be:

输出将是:

I have 1 ball

#1


128  

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语句中,用单个空格分隔项目:

>>> 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

#2


36  

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:

字符串的格式(Python 2.6和更新)方法也许是标准方法:

>>> births = str(5)
>>>
>>> print "there are {} births.".format(births)
there are 5 births.

This format method can be used with lists as well

此格式方法也可以与列表一起使用

>>> 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

#3


15  

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))

#4


8  

You can either use a formatstring:

您可以使用formatstring:

print "There are %d births" % (births,)

or in this simple case:

或者在这个简单的情况下:

print "There are ", births, "births"

#5


3  

On a current python version you have to use parenthesis, like so :

在当前的python版本中,您必须使用括号,如下所示:

print ("If there was a birth every 7 seconds", X)

#6


2  

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)

#7


1  

You can use string formatting to do this:

您可以使用字符串格式来执行此操作:

print "If there was a birth every 7 seconds, there would be: %d births" % births

or you can give print multiple arguments, and it will automatically separate them by a space:

或者你可以给多个参数打印,它会自动用空格分隔:

print "If there was a birth every 7 seconds, there would be:", births, "births"

#8


1  

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.

我希望这有帮助。

#9


0  

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:

方法1:

print('I have %d %s' %(a,b))

Method 2:

方法2:

print('I have',a,b)

Method 3:

方法3:

print('I have {} {}'.format(a,b))

Method 4:

方法4:

print('I have ' + str(a) +' ' +b)

Output would be:

输出将是:

I have 1 ball