Python的打印问题与空间
问题描述:
您好,我目前的工作目标,我想打印输出,例如在pythonPython的打印问题与空间
你好= 10
但我下面的代码打印出来这样
你好= 10
10是INT我尝试了这些代码,但没有工作
print "hello=",10
print "hello=",str(10)
print "hello=",str(10).strip()
我将不胜感激帮助谢谢
答
只需在连接字符串:
print "hello="+str(10)
答
使用str.format
,
print("hello={}".format(10))
PS:本print
声明已被替换因为Python 3.0 print()
功能。
Old: print x, # Trailing comma suppresses newline
New: print(x, end=" ") # Appends a space instead of a newline
有关详细说明,请参阅Print Is A Function。
答
如果您使用带有多个参数的print
,这些参数之间用,
分隔,则会在每个参数之间插入一个空格' '
作为分隔符。
当使用Python 3的print
function,您可以指定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的print
statement,还有就是尽我所知没有这样的选择的。
答
您也可以考虑使用Python 3兼容print()
功能:
此功能可以在__future__
指令后使用:
from __future__ import print_function
print("hello=", 10, sep='')
输出:
hello=10
的print()
功能一个月关键字参数,它允许您更换由空字符串分隔空间。
这里是在线帮助:
帮助的内置功能打印模块内建的:
打印(...) 打印(值,... 09月=”' ,end ='\ n',file = sys。标准输出,冲洗= FALSE)
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.
答
呼叫 “打印” 将放置一个逗号的空间。
是,Python提供了许多方法来打印字符串作为上面提到的,我还是想构建的输出与C或Java风格的格式:
print "hello=%d" % 10
这仅仅是你的代码编辑器的可视化方面,它不会影响您正在尝试执行的功能。你能提供你为什么需要这个的背景吗?你可以试试'print'hello = 10''' –
','形成一个打印分隔空间的元组。而是使用'print'hello =%s“%10”或更现代的“hello = {0}”格式(10)' –
这正是应该发生的事情。当你传递多个项目来打印时,将它们分隔开。如果你不想这样做,建立一个你想要的单个字符串,然后通过它。 – jonrsharpe