How to print a string without including '
' in Python
假设我的字符串是:
1 2 3 | ' Hai Hello Good eve ' |
如何消除中间的
'
1 | Hai Hello Good eve |
?
如果您不想在打印语句的末尾换行:
1 2 | import sys sys.stdout.write("text") |
您可以使用
1 2 3 4 5 6 7 8 9 | >>> a ="1 2" >>> print a 1 2 >>> a = a.replace(" ","") >>> print a 1 2 |
在Python 2.6中:
1 2 | print"Hello.", print"This is on the same line" |
在Python 3中
1 2 | print("Hello", end ="") print("This is on the same line") |
我意识到这是一个非常古老的帖子,但我也遇到了这个问题,我想在上面加上一点,以便澄清。我相信,原始用户要求的是如何让操作系统将字符串"some text some more text"解释为:
some text
some more text
而不仅仅是打印:
"some text
some more text"
答案是它已经在解释了。在Ubuntu中,我遇到了这样一个问题:在将换行符放入字符串时,我没有要求换行符进行转义。所以字符串:
"some text
some more text"
事实上
"some text\
some more text"
所需的只是使用
","
")
在"打印"后添加逗号:
1 2 | print"Hai Hello", print"Good eve", |
尽管"print"已在python 3.0中消失
很老的帖子,但似乎没有人能成功回答你的问题。两个可能的答案:
首先,由于转义的
Good eve\\\
Good eve\
","\
")
或者,您的字符串不是字符串,可能是元组。我只是有一个类似的错误,当我认为我有一个字符串,从来没有注意到它是如何打印的,因为它是一个长字符串。我的印刷体是:
("Lorem ipsum dolor sit amet, consectetur adipiscing elit.
Etiam orci felis, pulvinar id vehicula nec, iaculis eget quam.
Nam sapien eros, hendrerit et ullamcorper nec, mattis at ipsum.
Nulla nisi ante, aliquet nec fermentum non, faucibus vel odio.
Praesent ac odio vel metus condimentum tincidunt sed vitae magna.
Integer nulla odio, sagittis id porta commodo, hendrerit vestibulum risus.
...,"")
容易漏掉开头和结尾的括号,只需注意的括号。打印
1 2 3 4 5 | >>> 'Hai Hello Good eve '.replace(' ', ' ') 'Hai Hello Good eve ' |
不确定这是否是您所要求的,但您可以使用三重引号字符串:
1 2 3 4 5 6 | print"""Hey man And here's a new line you can put multiple lines inside this kind of string without using \ """ |
将打印:
1 2 3 4 5 | Hey man And here's a new line you can put multiple lines inside this kind of string without using |