假设我的字符串是:
' Hai Hello\nGood eve\n'
如何消除'\n'
介于两者之间并使字符串打印如下:
Hai Hello Good eve
?
如果您不想在print语句末尾添加换行符:
import sys sys.stdout.write("text")
您可以使用以下replace
方法:
>>> a = "1\n2" >>> print a 1 2 >>> a = a.replace("\n", " ") >>> print a 1 2
在Python 2.6中:
print "Hello.", print "This is on the same line"
在Python 3.0中
print("Hello", end = " ") print("This is on the same line")