Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/294.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/wix/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中打印包含变量的字符串_Python - Fatal编程技术网

在python中打印包含变量的字符串

在python中打印包含变量的字符串,python,Python,我是一名python初学者,虽然我知道打印包含字符串和变量的文本,但我想问一个关于这方面的基本问题。这是我的密码: x=5 print ("the value of x is ",x) print "the value of x is",x 第一

我是一名python初学者,虽然我知道打印包含字符串和变量的文本,但我想问一个关于这方面的基本问题。这是我的密码:

x=5                                                                                        
print ("the value of x is ",x)                                      
print "the value of x is",x

第一个打印命令打印
('x的值为',5)
,而第二个打印,
x的值为5
。但是
print('hello')
print'hello'
打印
hello
(相同),为什么?

因为
('hello')
只是
'hello'
,而不是一个元组。

因为
('hello')
只是
,而不是一个元组。

假设2.x,
print
是一条语句,逗号使表达式成为一个元组,用括号打印。假设Python3.x,
print
是一个函数,因此第一个正常打印,第二个是语法错误。

假设Python2.x,
print
是一个语句,逗号使表达式成为一个元组,用括号打印。假设Python3.x,
print
是一个函数,因此第一个函数正常打印,第二个函数是语法错误。

print是py2x not函数中的语句。所以打印
(“x的值是”,x)
实际上打印一个元组:

>>> type(('hello'))
<type 'str'>
>>> type(('hello',))  # notice the trailing `,` 
<type 'tuple'>
也可以导入py3x的打印功能:

>>> from __future__ import print_function
>>> print ("the value of x is","foo")
the value of x is foo

Print是py2x not函数中的语句。所以打印
(“x的值是”,x)
实际上打印一个元组:

>>> type(('hello'))
<type 'str'>
>>> type(('hello',))  # notice the trailing `,` 
<type 'tuple'>
也可以导入py3x的打印功能:

>>> from __future__ import print_function
>>> print ("the value of x is","foo")
the value of x is foo

想想看,如果
(x+y)+1
x+y
组成一个元组,那会毁掉一切!这就是为什么一个元组被做成像
('hello',)
这样,如果
(x+y)+1
x+y
做成一个元组,那会毁掉一切!这就是为什么一个元组被做成像
('hello',)
,后面是逗号,想在前面..修正了.打字错误,想在前面..修正了.谢谢,我理解你说的.你能建议我怎么做,或者继续使用python 2,或者跳过到3吗?@Ankushharma继续使用py2.7,因为大多数(外部)库仍然使用py2x,py2x和py3x之间的差异可以在这里找到:谢谢,我理解你说的话。你能建议我继续使用python 2还是跳过PY3吗?@Ankushharma继续使用py2.7,因为大多数(外部)库仍然在py2x上,py2x和py3x之间的差异可以在这里找到: