在Python中如何在同一行上打印变量和字符串?
我正在使用python计算如果一个孩子每7秒出生一次,5年内会有多少个孩子出生。问题在我的最后一行。当我将文本打印到变量的任一侧时,如何使变量工作 这是我的密码:在Python中如何在同一行上打印变量和字符串?,python,string,variables,printing,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 secon
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"
打印时使用
,
分隔字符串和变量:
print("If there was a birth every 7 seconds, there would be: ", births, "births")
,
在打印功能中,通过单个空格分隔项目:
>>> print("foo", "bar", "spam")
foo bar spam
或更好地使用:
字符串格式功能更强大,还允许您执行其他操作,如填充、填充、对齐、宽度、设置精度等
>>> print("{:d} {:03d} {:>20f}".format(1, 2, 1.1))
1 002 1.100000
^^^
0's padded to 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"
您可以使用格式字符串:
print "There are %d births" % (births,)
或者在这个简单的例子中:
print "There are ", births, "births"
再来两个
第一个
>>> births = str(5)
>>> print("there are " + births + " births.")
there are 5 births.
添加字符串时,它们会连接在一起
第二个
>>> births = str(5)
>>> print("there are " + births + " births.")
there are 5 births.
另外,字符串的格式
(Python 2.6及更新版本)方法可能是标准方法:
>>> births = str(5)
>>>
>>> print("there are {} births.".format(births))
there are 5 births.
此格式
方法也可用于列表
>>> format_list = ['five', 'three']
>>> # * unpacks the list:
>>> print("there are {} births and {} deaths".format(*format_list))
there are five births and three deaths
或字典
>>> format_dictionary = {'births': 'five', 'deaths': 'three'}
>>> # ** unpacks the dictionary
>>> print("there are {births} births, and {deaths} deaths".format(**format_dictionary))
there are five births, and three deaths
我将您的脚本复制并粘贴到.py文件中。我在Python2.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):
我希望这会有所帮助。在当前的python版本中,必须使用括号,如下所示:
print ("If there was a birth every 7 seconds", X)
如果您想使用python 3,它非常简单:
print("If there was a birth every 7 second, there would be %d births." % (births))
首先创建一个变量:例如:D=1。然后执行此操作,但将字符串替换为所需的内容:
D = 1
print("Here is a number!:",D)
Python是一种非常通用的语言。您可以使用不同的方法打印变量。我列举了以下五种方法。你可以根据自己的方便使用它们 示例:
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)
方法5:
print(f'I have {a} {b}')
产出将是:
我有一个球
使用
如果您正在进行玩具项目,请使用:
print('If there was a birth every 7 seconds, there would be:' births'births)
或
从Python3.6开始,您可以使用
稍有不同:使用Python 3并在同一行中打印几个变量:
print("~~Create new DB:",argv[5],"; with user:",argv[3],"; and Password:",argv[4]," ~~")
您可以使用f-string或.format()方法 使用f字串
print(f'If there was a birth every 7 seconds, there would be: {births} births')
使用.format()
如果您使用的是python 3.6或最新版本, f-string是最好、最简单的一款
print(f"{your_varaible_name}")
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))
在两者之间使用(逗号)
请参阅此代码以更好地理解:
# Weight converter pounds to kg
weight_lbs = input("Enter your weight in pounds: ")
weight_kg = 0.45 * int(weight_lbs)
print("You are ", weight_kg, " kg")
如果在字符串和变量之间使用逗号,如下所示:
print "If there was a birth every 7 seconds, there would be: ", births, "births"
使用字符串格式:
birth = 5.25487
print(f'''
{birth}
''')
谢谢你的回答,琥珀。你能解释一下%符号后面的“d”是什么吗?谢谢
%d
表示“将值格式化为整数”。类似地,%s
将是“将值格式化为字符串”,而%f
将是“将值格式化为浮点数”。我在回答中链接到的Python手册部分中记录了这些和更多内容。但是,如果使用第二种方法,请小心,因为这是一个元组,而不是字符串。字符串“输入您的名称:
忽略右引号打印(“您好,{}您的点是{}:”.format(用户名,点)
缺少右括号。决定与您的编程风格有关:M2是程序编程,M3是面向对象编程。M5的关键字是。如果需要,应使用M1和M4之类的字符串操作,这里不是这种情况(M1代表字典和元组;M4代表ascii艺术和其他格式化输出)2020年要小心(常识,我知道:D)。打印已经成为Python3中的一个功能,现在需要用括号括起来:Print(something)
(Python2今年已经过时了)。这并不能回答这个问题。一旦你有足够的数据,你就能够;相反-
print(f"{your_varaible_name}")
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))
# Weight converter pounds to kg
weight_lbs = input("Enter your weight in pounds: ")
weight_kg = 0.45 * int(weight_lbs)
print("You are ", weight_kg, " kg")
print "If there was a birth every 7 seconds, there would be: ", births, "births"
birth = 5.25487
print(f'''
{birth}
''')