带空格的Python打印问题

带空格的Python打印问题,python,printing,Python,Printing,您好,我目前正在做一些事情,我正在尝试用python打印输出 你好=10 但我下面的代码就是这样打印的 你好=10 10是一个整数,我试过这些代码,但都不起作用 print "hello=",10 print "hello=",str(10) print "hello=",str(10).strip() 如果您能提供帮助,我将不胜感激。只需将字符串连接起来: print "hello="+str(10) 使用 PS:Python3.0之后,print语句已被函数替换 Old: print

您好,我目前正在做一些事情,我正在尝试用python打印输出

你好=10

但我下面的代码就是这样打印的

你好=10

10是一个整数,我试过这些代码,但都不起作用

print "hello=",10
print "hello=",str(10)
print "hello=",str(10).strip()

如果您能提供帮助,我将不胜感激。

只需将字符串连接起来:

print "hello="+str(10)
使用


PS:Python3.0之后,
print
语句已被函数替换

Old: print x,           # Trailing comma suppresses newline
New: print(x, end=" ")  # Appends a space instead of a newline

有关详细说明,请参阅。

如果使用带有多个参数的
print
,,则会在每个参数之间插入一个空格
'

使用Python 3时,可以指定
sep
参数;默认值为

>>> from __future__ import print_function  # when in Python 2
>>> print("hello=", 10)
hello= 10
>>> print("hello=", 10, sep="")
hello=10
>>> print("hello=", 10, sep="###")
hello=###10

对于Python 2,我认为最好没有这样的选项。

< p>您也可以考虑使用Python 3兼容<代码>打印()/<代码>函数:

此功能可在
\uuuuu future\uuuu
指令后使用:

from __future__ import print_function

print("hello=", 10, sep='')
输出:

hello=10
print()
函数作为sep关键字参数,允许您用空字符串替换空格分隔符

以下是联机帮助:

关于内置函数打印模块内置内容的帮助:

打印(…) 打印(值,…,sep='',end='\n',file=sys.stdout,flush=False)

调用“print”将为逗号留出一个空格

是的,Python提供了许多方法来打印上面提到的字符串,我仍然希望使用C或Java样式的格式来构造输出:

print "hello=%d" % 10

这只是代码编辑器的一个可视化方面,它不会影响您尝试执行的功能。你能提供你为什么需要这个的背景吗?您可以尝试打印“hello=10”,形成一个元组,打印分隔的空格。而是使用
打印“hello=%s”%10
或更现代的
'hello={0}。格式(10)
,这正是应该发生的。当您传递多个要打印的项目时,会将它们隔开。如果你不想这样做,就创建一个你想要的字符串,并传递它。10是循环中传递的数字。我需要它作为我输出的视觉效果的一部分。谢谢你,保罗,这很有效!
Prints the values to a stream, or to sys.stdout by default.
Optional keyword arguments:
file:  a file-like object (stream); defaults to the current sys.stdout.
sep:   string inserted between values, default a space.
end:   string appended after the last value, default a newline.
flush: whether to forcibly flush the stream.
print "hello=%d" % 10