问题:如何在Python的同一行上打印变量和字符串?
我正在使用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"
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?
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"
回答 0
使用,
分隔字符串和变量,同时打印:
print "If there was a birth every 7 seconds, there would be: ",births,"births"
,
在print语句中将项目分隔一个空格:
>>> print "foo","bar","spam"
foo bar spam
或更好地使用字符串格式:
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
Use ,
to separate strings and variables while printing:
print("If there was a birth every 7 seconds, there would be: ", births, "births")
,
in print function separates 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
回答 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']
>>> print "there are {} births and {} deaths".format(*format_list) #unpack the list
there are five births and three deaths
或字典
>>> 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
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:
>>> 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']
>>> # * unpacks the list:
>>> print("there are {} births and {} deaths".format(*format_list))
there are five births and three deaths
or dictionaries
>>> 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是一种非常通用的语言。您可以通过不同的方法打印变量。我列出了以下4种方法。您可以根据需要使用它们。
例:
a=1
b='ball'
方法1:
print('I have %d %s' %(a,b))
方法2:
print('I have',a,b)
方法3:
print('I have {} {}'.format(a,b))
方法4:
print('I have ' + str(a) +' ' +b)
方法5:
print( f'I have {a} {b}')
输出为:
I have 1 ball
Python is a very versatile language. You may print variables by different methods. I have listed below five methods. You may use them according to your convenience.
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}')
The output would be:
I have 1 ball
回答 3
如果要使用python 3,它非常简单:
print("If there was a birth every 7 second, there would be %d births." % (births))
If you want to work with python 3, it’s very simple:
print("If there was a birth every 7 second, there would be %d births." % (births))
回答 4
从python 3.6开始,您可以使用文字字符串插值。
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
As of python 3.6 you can use 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
回答 5
您可以使用f-string或.format()方法
使用f弦
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))
You can either use the f-string or .format() methods
Using f-string
print(f'If there was a birth every 7 seconds, there would be: {births} births')
Using .format()
print("If there was a birth every 7 seconds, there would be: {births} births".format(births=births))
回答 6
您可以使用格式字符串:
print "There are %d births" % (births,)
或在这种简单情况下:
print "There are ", births, "births"
You can either use a formatstring:
print "There are %d births" % (births,)
or in this simple case:
print "There are ", births, "births"
回答 7
如果您使用的是python 3.6或最新版本,则f-string是最佳和简便的选择
print(f"{your_varaible_name}")
If you are using python 3.6 or latest,
f-string is the best and easy one
print(f"{your_varaible_name}")
回答 8
您将首先创建一个变量:例如:D =1。然后执行此操作,但是将字符串替换为所需的任何内容:
D = 1
print("Here is a number!:",D)
You would first make a variable: for example: D = 1. Then Do This but replace the string with whatever you want:
D = 1
print("Here is a number!:",D)
回答 9
在当前的python版本上,您必须使用括号,如下所示:
print ("If there was a birth every 7 seconds", X)
On a current python version you have to use parenthesis, like so :
print ("If there was a birth every 7 seconds", X)
回答 10
使用字符串格式
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
use String formatting
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
回答 11
您可以使用字符串格式来执行此操作:
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"
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"
回答 12
我将您的脚本复制并粘贴到.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
我希望这有帮助。
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:
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):
157680000
If there was a birth every 7 seconds, there would be: 22525714 births
I hope this helps.
回答 13
只需在之间使用,(逗号)。
请参阅以下代码以获得更好的理解:
# 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")
Just use , (comma) in between.
See this code for better understanding:
# 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")
回答 14
稍有不同:使用Python 3并在同一行中打印几个变量:
print("~~Create new DB:",argv[5],"; with user:",argv[3],"; and Password:",argv[4]," ~~")
Slightly different: Using Python 3 and print several variables in the same line:
print("~~Create new DB:",argv[5],"; with user:",argv[3],"; and Password:",argv[4]," ~~")
回答 15
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))
PYTHON 3
Better to use the format option
user_name=input("Enter your name : )
points = 10
print ("Hello, {} your point is {} : ".format(user_name,points)
or declare the input as string and use
user_name=str(input("Enter your name : ))
points = 10
print("Hello, "+user_name+" your point is " +str(points))
回答 16
如果在字符串和变量之间使用逗号,如下所示:
print "If there was a birth every 7 seconds, there would be: ", births, "births"
If you use a comma inbetween the strings and the variable, like this:
print "If there was a birth every 7 seconds, there would be: ", births, "births"