Suppose my string is:
假设我的字符串是:
' Hai Hello\nGood eve\n'
How do I eliminate the '\n'
in between and make a string print like :
如何消除两者之间的'\ n'并使字符串打印如下:
Hai Hello Good eve
?
8 个解决方案
#1
If you don't want the newline at the end of the print statement:
如果您不想在print语句末尾添加换行符:
import sys
sys.stdout.write("text")
#2
You can use the replace
method:
您可以使用replace方法:
>>> a = "1\n2"
>>> print a
1
2
>>> a = a.replace("\n", " ")
>>> print a
1 2
#3
In Python 2.6:
在Python 2.6中:
print "Hello.",
print "This is on the same line"
In Python 3.0
在Python 3.0中
print("Hello", end = " ")
print("This is on the same line")
#4
Add a comma after "print":
在“打印”后添加逗号:
print "Hai Hello",
print "Good eve",
Altho "print" is gone in Python 3.0
在Python 3.0中,“print”已经不复存在了
#5
I realize this is a terribly old post, but I just ran into this also and wanted to add to it for clarity. What the original user is asking, I believe, is how to get the OS to interpret the string "some text\nsome more text" as:
我意识到这是一个非常古老的帖子,但我也遇到了这个问题,并希望为了清晰起见而添加它。我相信原始用户要求的是如何让操作系统将字符串“some text \ nsome more text”解释为:
some text
some more text
更多文字
Instead of just printing:
而不仅仅是打印:
"some text\nsome more text"
“一些文字\ nsome更多文字”
and the answer is it is already interpreting. In ubuntu I ran into the problem of it escaping the newline characters when putting it into the string without me asking it to. So the string:
答案是它已经在解释了。在ubuntu中,我遇到了它在将新行字符放入字符串时转义它而没有我要求它的问题。所以字符串:
"some text\nsome more text"
“一些文字\ nsome更多文字”
is in fact
实际上是
"some text\\nsome more text"
“一些文字\\ nsome更多文字”
All that is required is to use the mystring.replace("\\\n", "\n")
and the desired output will be achieved. Hope this is clear and helps some future person.
所需要的只是使用mystring.replace(“\\\ n”,“\ n”)并实现所需的输出。希望这是明确的,并帮助一些未来的人。
#6
Way old post but nobody seemed to successfully answer your question. Two possible answers:
很老的帖子,但似乎没有人成功回答你的问题。两个可能的答案:
First, either your string is actually Hai Hello\\\\nGood eve\\\\n
printing as Hai Hello\\nGood eve\\n
due to the escaped \\\\
. Simple fix would be mystring.replace("\\\\n","\\n")
(See http://docs.python.org/reference/lexical_analysis.html#string-literals)
首先,你的字符串实际上是Hai Hello \\\\ nGood eve \\\\ n打印为Hai Hello \\ nGood eve \\ n由于逃脱了\\\\。简单的修复就是mystring.replace(“\\\\ n”,“\\ n”)(参见http://docs.python.org/reference/lexical_analysis.html#string-literals)
Or, your string isn't a string and possibly a tuple. I just had a similar error when I thought I had a string and never noticed how it was printing as it was a long string. Mine was printing as:
或者,您的字符串不是字符串,可能是元组。当我以为我有一个字符串并且从未注意到它是如何打印时,我只是遇到了类似的错误,因为它是一个长字符串。我的印刷是:
("Lorem ipsum dolor sit amet, consectetur adipiscing elit.\nEtiam orci felis, pulvinar id vehicula nec, iaculis eget quam.\nNam sapien eros, hendrerit et ullamcorper nec, mattis at ipsum.\nNulla nisi ante, aliquet nec fermentum non, faucibus vel odio.\nPraesent ac odio vel metus condimentum tincidunt sed vitae magna.\nInteger nulla odio, sagittis id porta commodo, hendrerit vestibulum risus.\n...,"")
(“Lorem ipsum dolor sit amet,consectetur adipiscing elit。\ nEtiam orci felis,pulvinar id vehicula nec,iaculis eget quam。\ nNam sapien eros,hendrerit et ullamcorper nec,mattis at ipsum。\ nNulla nisi ante,aliquet nec fermentum non, faucibus vel odio。\ nPraesent ac odio vel metin condimentum tincidunt sed vitae magna。\ nInteger nulla odio,sagittis id porta commodo,hendrerit vestibulum risus。\ n ...,“”)
Easy to miss the brackets at the start and end and just notice the \n's. Printing mystring[0]
should solve this (or whatever index it is in the list/tuple etc).
很容易错过开头和结尾的括号,只需注意\ n's。打印mystring [0]应解决此问题(或列表/元组中的任何索引等)。
#7
>>> 'Hai Hello\nGood eve\n'.replace('\n', ' ')
'Hai Hello Good eve '
#8
Not sure if this is what you're asking for, but you can use the triple-quoted string:
不确定这是否是您要求的,但您可以使用三引号字符串:
print """Hey man
And here's a new line
you can put multiple lines inside this kind of string
without using \\n"""
Will print:
Hey man And here's a new line you can put multiple lines inside this kind of string without using \n
#1
If you don't want the newline at the end of the print statement:
如果您不想在print语句末尾添加换行符:
import sys
sys.stdout.write("text")
#2
You can use the replace
method:
您可以使用replace方法:
>>> a = "1\n2"
>>> print a
1
2
>>> a = a.replace("\n", " ")
>>> print a
1 2
#3
In Python 2.6:
在Python 2.6中:
print "Hello.",
print "This is on the same line"
In Python 3.0
在Python 3.0中
print("Hello", end = " ")
print("This is on the same line")
#4
Add a comma after "print":
在“打印”后添加逗号:
print "Hai Hello",
print "Good eve",
Altho "print" is gone in Python 3.0
在Python 3.0中,“print”已经不复存在了
#5
I realize this is a terribly old post, but I just ran into this also and wanted to add to it for clarity. What the original user is asking, I believe, is how to get the OS to interpret the string "some text\nsome more text" as:
我意识到这是一个非常古老的帖子,但我也遇到了这个问题,并希望为了清晰起见而添加它。我相信原始用户要求的是如何让操作系统将字符串“some text \ nsome more text”解释为:
some text
some more text
更多文字
Instead of just printing:
而不仅仅是打印:
"some text\nsome more text"
“一些文字\ nsome更多文字”
and the answer is it is already interpreting. In ubuntu I ran into the problem of it escaping the newline characters when putting it into the string without me asking it to. So the string:
答案是它已经在解释了。在ubuntu中,我遇到了它在将新行字符放入字符串时转义它而没有我要求它的问题。所以字符串:
"some text\nsome more text"
“一些文字\ nsome更多文字”
is in fact
实际上是
"some text\\nsome more text"
“一些文字\\ nsome更多文字”
All that is required is to use the mystring.replace("\\\n", "\n")
and the desired output will be achieved. Hope this is clear and helps some future person.
所需要的只是使用mystring.replace(“\\\ n”,“\ n”)并实现所需的输出。希望这是明确的,并帮助一些未来的人。
#6
Way old post but nobody seemed to successfully answer your question. Two possible answers:
很老的帖子,但似乎没有人成功回答你的问题。两个可能的答案:
First, either your string is actually Hai Hello\\\\nGood eve\\\\n
printing as Hai Hello\\nGood eve\\n
due to the escaped \\\\
. Simple fix would be mystring.replace("\\\\n","\\n")
(See http://docs.python.org/reference/lexical_analysis.html#string-literals)
首先,你的字符串实际上是Hai Hello \\\\ nGood eve \\\\ n打印为Hai Hello \\ nGood eve \\ n由于逃脱了\\\\。简单的修复就是mystring.replace(“\\\\ n”,“\\ n”)(参见http://docs.python.org/reference/lexical_analysis.html#string-literals)
Or, your string isn't a string and possibly a tuple. I just had a similar error when I thought I had a string and never noticed how it was printing as it was a long string. Mine was printing as:
或者,您的字符串不是字符串,可能是元组。当我以为我有一个字符串并且从未注意到它是如何打印时,我只是遇到了类似的错误,因为它是一个长字符串。我的印刷是:
("Lorem ipsum dolor sit amet, consectetur adipiscing elit.\nEtiam orci felis, pulvinar id vehicula nec, iaculis eget quam.\nNam sapien eros, hendrerit et ullamcorper nec, mattis at ipsum.\nNulla nisi ante, aliquet nec fermentum non, faucibus vel odio.\nPraesent ac odio vel metus condimentum tincidunt sed vitae magna.\nInteger nulla odio, sagittis id porta commodo, hendrerit vestibulum risus.\n...,"")
(“Lorem ipsum dolor sit amet,consectetur adipiscing elit。\ nEtiam orci felis,pulvinar id vehicula nec,iaculis eget quam。\ nNam sapien eros,hendrerit et ullamcorper nec,mattis at ipsum。\ nNulla nisi ante,aliquet nec fermentum non, faucibus vel odio。\ nPraesent ac odio vel metin condimentum tincidunt sed vitae magna。\ nInteger nulla odio,sagittis id porta commodo,hendrerit vestibulum risus。\ n ...,“”)
Easy to miss the brackets at the start and end and just notice the \n's. Printing mystring[0]
should solve this (or whatever index it is in the list/tuple etc).
很容易错过开头和结尾的括号,只需注意\ n's。打印mystring [0]应解决此问题(或列表/元组中的任何索引等)。
#7
>>> 'Hai Hello\nGood eve\n'.replace('\n', ' ')
'Hai Hello Good eve '
#8
Not sure if this is what you're asking for, but you can use the triple-quoted string:
不确定这是否是您要求的,但您可以使用三引号字符串:
print """Hey man
And here's a new line
you can put multiple lines inside this kind of string
without using \\n"""
Will print:
Hey man And here's a new line you can put multiple lines inside this kind of string without using \n