简体   繁体   English

是否可以在没有新线或空间的情况下进行打印?

[英]Is it possible to print without new line or space?

Let's say that I want to print 'hello' by 2 different print statements. 假设我想用2种不同的打印语句打印“你好”。

Like: 喜欢:

print "hel" 
print "lo"

But python automatically prints new line so if we want it in the same line we need to - 但是python会自动打印新行,所以如果我们想要它在同一行,我们需要 -

print "hel"**,**

Here's the problem - the , makes a space and I want it connected. 这是问题 - ,创造一个空间,我希望它连接起来。 Thanks. 谢谢。

You can use the print function 您可以使用print 功能

>>> from __future__ import print_function
>>> print('hel', end=''); print('lo', end='')
hello

Obviously the semicolon is in the code only to show the result in the interactive interpreter. 显然分号只在代码中显示交互式解释器中的结果。

The end keyword parameter to the print function specifies what you want to print after the main text. print函数的end关键字参数指定要在主文本后打印的内容。 It defaults to '\\n' . 它默认为'\\n' Here we change it to '' so that nothing is printed on the end. 在这里,我们将其更改为''以便最后不打印任何内容。

>>> import sys
>>> sys.stdout.write('hel');sys.stdout.write('lo')
hello

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM