Bob*_*Uni 143 python printing string variables
如果孩子每7秒钟出生一次,我正在使用python来计算出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"
Run Code Online (Sandbox Code Playgroud)
Ash*_*ary 217
用于,
在打印时分隔字符串和变量:
print "If there was a birth every 7 seconds, there would be: ",births,"births"
Run Code Online (Sandbox Code Playgroud)
,
在print语句中用单个空格分隔项目:
>>> print "foo","bar","spam"
foo bar spam
Run Code Online (Sandbox Code Playgroud)
或更好地使用字符串格式:
print "If there was a birth every 7 seconds, there would be: {} births".format(births)
Run Code Online (Sandbox Code Playgroud)
字符串格式化功能更强大,并允许您执行其他一些操作,例如:填充,填充,对齐,宽度,设置精度等
>>> print "{:d} {:03d} {:>20f}".format(1,2,1.1)
1 002 1.100000
^^^
0's padded to 2
Run Code Online (Sandbox Code Playgroud)
演示:
>>> 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
Run Code Online (Sandbox Code Playgroud)
Teh*_*ris 49
还有两个
第一个
>>>births = str(5)
>>>print "there are " + births + " births."
there are 5 births.
Run Code Online (Sandbox Code Playgroud)
添加字符串时,它们会连接起来.
第二个
此外,format
(Python 2.6和更新)字符串方法可能是标准方式:
>>> births = str(5)
>>>
>>> print "there are {} births.".format(births)
there are 5 births.
Run Code Online (Sandbox Code Playgroud)
此format
方法也可以与列表一起使用
>>> format_list = ['five','three']
>>> print "there are {} births and {} deaths".format(*format_list) #unpack the list
there are five births and three deaths
Run Code Online (Sandbox Code Playgroud)
或词典
>>> 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
Run Code Online (Sandbox Code Playgroud)
小智 27
如果你想使用python 3,它很简单:
print("If there was a birth every 7 second, there would be %d births." % (births))
Run Code Online (Sandbox Code Playgroud)
小智 22
Python是一种非常通用的语言.您可以通过不同的方法打印变量.我列出了以下4种方法.您可以根据自己的方便使用它们.
例:
a=1
b='ball'
Run Code Online (Sandbox Code Playgroud)
方法1:
print('I have %d %s' %(a,b))
Run Code Online (Sandbox Code Playgroud)
方法2:
print('I have',a,b)
Run Code Online (Sandbox Code Playgroud)
方法3:
print('I have {} {}'.format(a,b))
Run Code Online (Sandbox Code Playgroud)
方法4:
print('I have ' + str(a) +' ' +b)
Run Code Online (Sandbox Code Playgroud)
输出将是:
I have 1 ball
Run Code Online (Sandbox Code Playgroud)
enp*_*nax 12
您可以使用formatstring:
print "There are %d births" % (births,)
Run Code Online (Sandbox Code Playgroud)
或者在这个简单的情况下:
print "There are ", births, "births"
Run Code Online (Sandbox Code Playgroud)
Pab*_*rre 10
从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
Run Code Online (Sandbox Code Playgroud)
从Python-3.8开始,您可以使用带有变量名称打印的f字符串!
age = 19
vitality = 17
charisma = 16
name = "Alice"
print(f"{name=}, {age=}, {vitality=}, {charisma=}")
# name='Alice', age=19, vitality=17, charisma=16
Run Code Online (Sandbox Code Playgroud)
charisma
还是vitality
?好吧,你不在乎,无论如何它都是正确的。小智 5
您可以使用f-string或.format()方法
使用f弦
print(f'If there was a birth every 7 seconds, there would be: {births} births')
Run Code Online (Sandbox Code Playgroud)
使用.formt()
print("If there was a birth every 7 seconds, there would be: {births} births".format(births=births))
Run Code Online (Sandbox Code Playgroud)
小智 5
如果您使用的是 python 3.6 或最新版本,f-string 是最好且简单的
print(f"{your_varaible_name}")
Run Code Online (Sandbox Code Playgroud)