在Python中打印单变量和多变量?
在本节中,我们将检查在两个不同的python版本中打印单变量和多变量输出。
#Python2.7
打印单个变量
>>> #Python 2.7
>>> #Print single variable
>>> print 27
27
>>> print "Rahul"
Rahul
>>> #Print single variable, single brackets
>>> print(27)
27
>>> print("Rahul")
RahulPython3.6
>>> #Python 3.6 >>> #Print single variable without brackets >>> print 27 SyntaxError: Missing parentheses in call to 'print' >>> print "Rahul" SyntaxError: Missing parentheses in call to 'print'
上面3.6中的语法,是由于:在python3.x中,print不是语句,而是函数(print())。因此,将print更改为print()。
>>> print (27)
27
>>> print("Rahul")
Rahul打印多个变量
Python2.x(例如:python2.7)
>>> #Python 2.7
>>> #Print multiple variables
>>> print 27, 54, 81
27 54 81
>>> #Print multiple variables inside brackets
>>> print (27, 54, 81)
(27, 54, 81)
>>> #With () brackets, above is treating it as a tuple, and hence generating the
>>> #tuple of 3 variables
>>> print ("Rahul", "Raj", "Rajesh")
('Rahul', 'Raj', 'Rajesh')
>>>因此,从上面的输出中,我们可以在python2.x中看到,在方括号()中传递多个变量,会将其视为多个项目的元组
Python3.x(例如:python3.6)
#Python 3.6
#Print multiple variables
>>> print(27, 54, 81)
27 54 81
>>> print ("Rahul", "Raj", "Rajesh")
Rahul Raj Rajesh让我们再来看一个python2.x和python3.x中多个语句的示例
热门推荐
1 训练狗狗工作
4 狗狗定点排便训练
5 狗狗训练吠叫
6 狗狗尿垫训练
7 狗狗训练座
8 狗狗训练攻略
9 狗狗训练记
10 做训练狗狗
11 狗狗训练词
12 狗狗谢谢怎么训练
13 狗狗训练如厕
14 狗狗太凶怎么训练
15 狗狗训练它
16 狗狗克制训练
17 训练基地狗狗
18 怎么样训练狗狗大小硬