如何在Python中的同一行上打印variables和string?
如果孩子每7秒出生一次,我就用python来计算出5年后会出生多less个孩子。 问题在于我的最后一行。 当我打印文本的任何一面时,如何获得一个variables的工作?
这是我的代码:
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"
在打印时使用,
分隔string和variables:
print "If there was a birth every 7 seconds, there would be: ",births,"births"
,
在print语句中,由单个空格分隔项目:
>>> print "foo","bar","spam" foo bar spam
或者更好的使用string格式 :
print "If there was a birth every 7 seconds, there would be: {} births".format(births)
string格式化function更强大,并允许你做一些其他的事情,如:填充,填充,alignment,宽度,设置精度等
>>> 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
另外两个
第一个
>>>births = str(5) >>>print "there are " + births + " births." there are 5 births.
添加string时,它们连接在一起。
第二个
此外,string的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
如果你想使用Python 3,这是非常简单的:
print("If there was a birth every 7 second, there would be %d births." % (births))
你可以使用一个formatstring:
print "There are %d births" % (births,)
或者在这个简单的情况下:
print "There are ", births, "births"
在当前的Python版本中,您必须使用括号,如下所示:
打印(“如果每7秒出生一次”,X)
我将你的脚本复制并粘贴到.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
我希望这有帮助。
你可以使用string格式来做到这一点:
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"
你将首先做一个variables:例如:D = 1然后做这个,但用任何你想要的stringreplace:
D = 1 print("Here is a number!:",D)