2013-06-17 11 views
65

私は子供が7秒ごとに生まれた場合、5年間で何人の子供が生まれるかを計算するためにpythonを使用しています。問題は私の最後の行です。どのように私はそれのどちらかの側にテキストを印刷しているときに変数を動作させるようにするのですか?ここで変数と文字列をPythonの同じ行に出力する方法はありますか?

は私のコードです:

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" 

答えて

114

使用,印刷中の文字列と変数を分離するために:

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

,をprint文では、単一のスペースでアイテムをsepartes:

>>> print "foo","bar","spam" 
foo bar spam 

以上string formatting

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 
1

これを行うには、string formattingを使用することができます。

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" 
+0

アンバーにお返事ありがとうございます。 '%d'が%記号の後に何をするのか説明できますか?ありがとうございました –

+2

'%d 'は"フォーマット値を整数として "を意味します。同様に、 '%s'は"文字列としてのフォーマット値 "、"%f "は"浮動小数点数としてのフォーマット値 "です。これ以上のことは、私の答えでリンクされているPythonマニュアルの部分に書かれています。 – Amber

+0

ありがとうございます。 –

5

あなたはformatStringのを使用することができ、次のいずれか

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

またはこの単純なケースでは:

print "There are ", births, "births" 
+0

それはタプルであり、文字列ではないので、その2番目の方法を使用する場合は注意してください。 – TehTris

28

さらに2

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

出力(パイソン2.7.10)であった。

157680000 
If there was a birth every 7 seconds, there would be: 22525714 births 

これが役立つことを願っています。そのようなあなたは、括弧を使用する必要があり、現在のPythonのバージョンに

3

、:

プリント(「出産があった場合は7秒ごと」、X)

10

あなたは、Python 3で動作するようにしたい場合は、それは非常に簡単です:

print("If there was a birth every 7 second, there would be %d births." % (births)) 
2

あなたが最初の変数になるだろう:例えば:D = 1は、その後、これを行うが、あなたが好きで文字列を置換:

D = 1 
print("Here is a number!:",D) 
関連する問題