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

,印刷機能では、アイテムを単一のスペースで区切ります。

>>> 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 に答える
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 に答える
3

現在の python バージョンでは、次のように括弧を使用する必要があります。

print ("If there was a birth every 7 seconds", X)
于 2016-07-12T05:49:33.010 に答える
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 に答える
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

文字列と変数の間にカンマを使用すると、次のようになります。

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