如何在Python中的同一行上打印变量和字符串?

时间:2013-06-17 17:58:08

标签: python string variables printing

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

18 个答案:

答案 0 :(得分:186)

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

print "If there was a birth every 7 seconds, there would be: ",births,"births"
print语句中的

,将项目按单个空格分开:

>>> 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 :(得分:45)

另外两个

第一个

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

答案 2 :(得分:21)

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

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

答案 3 :(得分:11)

您可以使用formatstring:

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

或在这个简单的情况下:

print "There are ", births, "births"

答案 4 :(得分:10)

Python是一种非常通用的语言。您可以通过不同的方法打印变量。我列出了以下4种方法。您可以根据需要方便地使用它们。

示例:

a=1
b='ball'

方法1:

print('I have %d %s' %(a,b))

方法2:

print('I have',a,b)

方法3:

print('I have {} {}'.format(a,b))

方法4:

print('I have ' + str(a) +' ' +b)

输出为:

I have 1 ball

答案 5 :(得分:7)

从python 3.6开始,您可以使用Literal String Interpolation.

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

答案 6 :(得分:3)

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

print ("If there was a birth every 7 seconds", X)

答案 7 :(得分:3)

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

D = 1
print("Here is a number!:",D)

答案 8 :(得分:2)

您可以使用 f-string .format()方法

使用f字符串

print(f'If there was a birth every 7 seconds, there would be: {births} births')

使用.formt()

print("If there was a birth every 7 seconds, there would be: {births} births".format(births=births))

答案 9 :(得分: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"

答案 10 :(得分:1)

如果您使用的是python 3.6或最新版本, f弦是最好的和容易的

print(f"{your_varaible_name}")

答案 11 :(得分:1)

使用String formatting

cxt.drawRectangle(x,y,width,height,{type: 'fill',
           color: '#eeeeee',
           opacity: 0.9});  

如果您要进行玩具项目,请使用:

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) 

答案 12 :(得分: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

我希望这会有所帮助。

答案 13 :(得分:0)

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

print("~~Create new DB:",argv[5],"; with user:",argv[3],"; and Password:",argv[4]," ~~")

答案 14 :(得分:0)

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

答案 15 :(得分:0)

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

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

重量转换器磅到千克

weight_lbs = input(“以磅为单位输入您的体重:”)

weight_kg = 0.45 * int(weight_lbs)

print(“您是”,weight_kg,“ kg”)

答案 16 :(得分:0)

使用字符串格式:

birth = 5.25487
print(f'''
{birth} 
''')

答案 17 :(得分:-1)

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

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