views:

2555

answers:

7

Suppose my string is ' Hai Hello\nGood eve\n' How to eliminate the '\n' in between and make a string print like : Hai Hello

Good eve

???

+7  A: 

You can use the replace method:

>>> a = "1\n2"
>>> print a
1
2
>>> a = a.replace("\n", " ")
>>> print a
1 2
Torsten Marek
+2  A: 

If you don't want the newline at the end of the print statement:

import sys
sys.stdout.write("text")
Staale
I felt the information was related to the question at hand. And there where already good answers.
Staale
I think you should also include what is asked for, so that your answer can be read in isolation. Or at least you should include a reference to the most appropriate other answer.
Douglas Leeder
The information he posted is still relevant to the topic, why do you all have to be a bunch of Nazis? Geez you people are harsh.
Justin Bennett
To Justin Bennett with greetings from germany: "YOU don't know what 'bunch of Nazis' mean !"
Blauohr
The question changed. Please delete all of these comments.
S.Lott
+1  A: 
>>> 'Hai Hello\nGood eve\n'.replace('\n', ' ')
'Hai Hello Good eve '
DzinX
+2  A: 

Add a comma after "print":

print "Hai Hello",
print "Good eve",

Altho "print" is gone in Python 3.0

chacmool
print isn't "gone" in Python 3; rather, it is changed from a statement to a function call.
DLJessup
A: 

If you want to remove new lines and to be platform independent:

string.replace(os.linesep, "")
Martin Burger
that's just wrong
SilentGhost
+1  A: 

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
hasen j
+1  A: 

In Python 2.6:

print "Hello.",
print "This is on the same line"

In Python 3.0

print("Hello", end = " ")
print("This is on the same line")
Andrew Marsh