输出中的括号和引号 [英] Parentheses and quotation marks in output

查看:227
本文介绍了输出中的括号和引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时,当我使用打印功能时,输出中会出现括号和引号.我正在使用Python 3.4,并在Mac上的Sublime Text中编写代码.

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.

这是一个例子

输入:

a=2
print("a",a)

输出:

('a', 2)

我只想显示a和2.

提前谢谢!

推荐答案

您似乎正在使用Python 2.

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

应该为您提供所需的结果.或者,使用更新的 str.format() 方法:

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

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

在Python 3中,您的语句print("a",a)将按预期工作.在Sublime中检查您的构建系统,以确保您正在调用python3而不是python.运行以下代码以查看实际使用的版本:

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)

要创建Python 3构建系统,请使用JSON语法和以下内容打开一个新文件:

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"
}

将文件另存为Packages/User/Python3.sublime-build,其中Packages是选择 Sublime Text -> Preferences -> Browse Packages... 时打开的文件夹.现在,您可以选择 Tools -> Build System -> Python3 ,并假设PATH中包含python3,则应使用正确的版本进行构建.

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.

如果构建失败并出现找不到python3的错误,请打开终端并输入

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

which python3

查看安装位置.复制整个路径并将其放入构建系统中.例如,如果which python3返回/usr/local/bin/python3,则.sublime-build文件中的"cmd"语句应为:

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"],

这篇关于输出中的括号和引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆