【问题标题】:Methods for printing without adding control characters in python [duplicate]在python中不添加控制字符的打印方法[重复]
【发布时间】:2013-12-21 05:30:26
【问题描述】:

我一直在使用函数sys.stdout.write(string),但我想知道是否有其他方法可以达到此目的。提前致谢!

【问题讨论】:

  • 您使用的是哪个版本的 Python?
  • 对不起,我忘了,python 2.7
  • 也许最好说“没有控制字符”...@sweeneyrod
  • 也许,这对我来说似乎不太清楚,因为在 Python 2.x 中,print 语句之后打印的唯一内容是换行符和空格,但我不会改回来。

标签: python python-2.7 python-2.x


【解决方案1】:

Python 3.x:

print(string, end="")

Python 2.x:

from __future__ import print_function
print(string, end="")

print string,    # This way adds a space at the end.

从重复问题的第二个答案中,我得到了这个想法:

而不是这样的:

>>> for i in xrange(10):
        print i,
1 2 3 4 5 6 7 8 9 10

你也许可以这样做:

>>> numbers = []
>>> for i in xrange(10):
       numbers.append(i)
>>> print "".join(map(str, numbers))
12345678910

我会推荐importing print_function。或者(厚颜无耻的回答)升级到 Python 3.x!

【讨论】:

  • 谢谢,问题是我也不想要额外的空间,我会编辑我的问题;)
猜你喜欢
  • 2016-06-30
  • 1970-01-01
  • 2016-07-23
  • 2017-08-13
  • 2013-08-12
  • 1970-01-01
  • 2021-01-14
  • 2020-10-19
  • 2023-03-10
相关资源
最近更新 更多