Parentheses and quotation marks in output

跟風遠走 提交于 2019-12-31 02:48:06

问题


Sometimes when I use the print function, parentheses and quotation marks appear in the output. I'm using Python 3.4 and writing the code in Sublime Text on a mac.

Here's an example

Input:

a=2
print("a",a)

Output:

('a', 2)

I'd like to show only a and 2.

Thanks in advance!


回答1:


You appear to be using Python 2.

a = 2
print("a %i" % a)

should give you the results you're looking for. Or, using the newer str.format() method:

print("a {}".format(a))

In Python 3, your statement print("a",a) will work as expected. Check your build system in Sublime to make sure you're calling python3 instead of python. Run this code to see what version is actually being used:

import sys

print(sys.version)

To create a Python 3 build system, open a new file with JSON syntax and the following contents:

{
    "cmd": ["python3", "-u", "$file"],
    "file_regex": "^[ ]*File \"(...*?)\", line ([0-9]*)",
    "selector": "source.python"
}

Save the file as Packages/User/Python3.sublime-build where Packages is the folder opened when you select Sublime Text -> Preferences -> Browse Packages.... You can now select Tools -> Build System -> Python3 and, assuming python3 is in your PATH, you should build with the correct version.

If the build fails with an error that it can't find python3, open Terminal and type

which python3

to see where it's installed. Copy the entire path and put it in the build system. For example, if which python3 returns /usr/local/bin/python3, then the "cmd" statement in your .sublime-build file should be:

"cmd": ["/usr/local/bin/python3", "-u", "$file"],



回答2:


Are you sure you are executing it on Python 3 interpreter? In Python 2 print is an statment so it takes no parentheses

print ("a", 2) // parentheses are interpreted as a tuple constructor
>>> ('a', 2)

is the same as

print tuple(["a",2])
>>> ('a', 2)

or in Python 3:

print( ("a",2) )
>>> ('a', 2)



回答3:


I think you are using python 2. In python 2 you don't need parentheses and directly write code as below

print "a", a


来源:https://stackoverflow.com/questions/28116528/parentheses-and-quotation-marks-in-output

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!