221

我正在使用 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"
4

17 回答 17

332

打印时用于,分隔字符串和变量:

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

,in 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
于 2013-06-17T17:58:57.770 回答
120

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
于 2018-07-05T09:20:02.080 回答
65

还有两个

第一个

>>> 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
于 2013-06-17T18:06:51.940 回答
29

如果你想使用 python 3,这很简单:

print("If there was a birth every 7 second, there would be %d births." % (births))
于 2016-08-14T10:53:04.100 回答
19

您可以使用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))
于 2019-02-12T08:07:57.567 回答
18

从 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
于 2018-07-18T16:19:53.070 回答
12

您可以使用格式字符串:

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

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

print "There are ", births, "births"
于 2013-06-17T17:59:29.380 回答
7

如果您使用的是 python 3.6 或最新版本,f-string 是最好和最简单的

print(f"{your_varaible_name}")
于 2019-04-24T10:49:57.410 回答
3

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

print ("If there was a birth every 7 seconds", X)
于 2016-07-12T05:49:33.010 回答
3

您将首先创建一个变量:例如:D = 1。然后执行此操作,但将字符串替换为您想要的任何内容:

D = 1
print("Here is a number!:",D)
于 2017-01-30T17:17:53.343 回答
2

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

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"
于 2013-06-17T17:59:11.200 回答
2

使用字符串格式

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
于 2018-07-18T05:40:02.913 回答
1

我将您的脚本复制并粘贴到 .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

我希望这有帮助。

于 2016-06-17T18:21:54.607 回答
1

只需在两者之间使用 , (逗号)。

请参阅此代码以更好地理解:

# 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")
于 2019-07-21T14:32:02.440 回答
-1

略有不同:使用 Python 3 并在同一行中打印多个变量:

print("~~Create new DB:",argv[5],"; with user:",argv[3],"; and Password:",argv[4]," ~~")
于 2019-01-25T12:18:36.163 回答
-1

蟒蛇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))
于 2019-07-16T07:42:09.473 回答
-1

如果在字符串和变量之间使用逗号,如下所示:

print "If there was a birth every 7 seconds, there would be: ", births, "births"
于 2020-02-19T23:08:12.763 回答