Python打印空间问题

时间:2017-02-14 23:00:55

标签: python printing

您好我正在处理某些事情,我正在尝试在python中打印输出

  

您好= 10

但我的代码是打印出来的

  

你好= 10

10是一个int我试过这些代码但没有工作

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

我将非常感谢帮助谢谢

5 个答案:

答案 0 :(得分:3)

简单地连接字符串:

print "hello="+str(10)

答案 1 :(得分:3)

使用str.format

print("hello={}".format(10))

PS:自Python 3.0起,print语句已被print()函数替换。

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

有关详细说明,请参阅Print Is A Function

答案 2 :(得分:1)

如果您使用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,据我所知,没有这样的选择。

答案 3 :(得分:0)

您也可以考虑使用兼容Python 3的print()函数:

此功能可在__future__指令后使用:

from __future__ import print_function

print("hello=", 10, sep='')

输出:

hello=10

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

以下是在线帮助:

  

有关内置模块中内置函数打印的帮助:

     

打印(...)       print(value,...,sep ='',end =' \ n',file = sys.stdout,flush = 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.

答案 4 :(得分:0)

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

是的,Python提供了很多方法来打印如上所述的字符串,我仍然想用C或Java样式格式构造输出:

for( x=1 , y=1 ; x<6 , y<10 ; x++  , y++){
    cout << "x:" << x << endl;
    cout << "y:" << y << endl;