2013-10-22 48 views
1

对不起我想学习Python,但我怎么打印此行,这些(”,不会被打印蟒纹格式

代码:

y = 7 
z = 7 - y 
print('you need ', z, 'more years of citizenship to become a US representative') 

结果:

('you need ', 6, 'more years of citizenship to become a US representative') 

,但我不想不必要parenthesizes,逗号和空格怪异。

感谢

+0

好像你正在使用Python-2.x。尝试'打印'你需要',z'多年的国籍成为美国代表''(去除周围的括号)。 – falsetru

+0

@aIKid哪个输入? – glglgl

+0

@glglgl从不知道。我的错。 – aIKid

回答

1

你用括号包围它创建两个字符串和数量的tuple。然后,print接收元组并使用它始终用于元组的特殊格式打印它。 print在python 2.7.x是一个关键字,而不是一个功能,所以你不使用它的括号。

2

您正在使用Python2为什么括号和逗号获得打印出来的理由是:
你有print后什么是一个元组,分别是:

('you need ', z, 'more years of citizenship to become a US representative') 

这三个要素和Python的元组会以元组的形式打印出来,所以就是括号和逗号。
在Python 3中,括号不会被打印出来,因为print从语言结构(或使用他们自己的单词“语句”)更改为一个函数,并且需要在其参数上使用括号。

要改变它在python2工作:

print 'you need ', z, 'more years of citizenship to become a US representative' 

print ('you need ' + str(z) + 'more years of citizenship to become a US representative') 
1

试试这个

print 'you need {0} more years of citizenship to ...'.format(z) 
1

你应该使用字符串格式化:

print 'you need %d more years of citizenship to become a US representative' % z' 

它将以Z

0

的值替代%d(表示数字)为了给Python3准备,你可以在最高层

from __future__ import print_function 

添加到您的脚本,然后使用print作为你在你的问题中所做的功能。