当我在python + jupyter中使用print时,仍会显示引号

问题描述 投票:1回答:2

像这样的简单代码:

print("sfjeorf",4,"fefa",5,)

我使用python在jupyter中运行它。结果是:

('sfjeorf', '4', 'fefa', '5')

enter image description here

我应该做什么来摆脱引号和括号,以便结果显示如下:

sfjeorf4fefa5
python string printing jupyter
2个回答
4
投票

你正在使用Python 2,它不会在参数周围使用(),因此它认为你正在打印一个元组。使用以下内容或切换到Python 3,其中print成为函数而不是语句。

print "sfjeorf",4,"fefa",5

摆脱空间以获得您要求的输出是更棘手的。 Python 2中最简单的方法是导入print函数实现:

>>> from __future__ import print_function
>>> print("sfjeorf",4,"fefa",5)
sfjeorf 4 fefa 5
>>> print("sfjeorf",4,"fefa",5,sep='')
sfjeorf4fefa5

0
投票

在这种情况下,我更喜欢使用string.format()来更好地控制输出。看看代码将如何:

>>> print('{}{}{}{}'.format("sfjeorf",4,"fefa",5,))
sfjeorf4fefa5
© www.soinside.com 2019 - 2024. All rights reserved.