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
???
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
???
You can use the replace
method:
>>> a = "1\n2"
>>> print a
1
2
>>> a = a.replace("\n", " ")
>>> print a
1 2
If you don't want the newline at the end of the print statement:
import sys
sys.stdout.write("text")
>>> 'Hai Hello\nGood eve\n'.replace('\n', ' ')
'Hai Hello Good eve '
Add a comma after "print":
print "Hai Hello",
print "Good eve",
Altho "print" is gone in Python 3.0
If you want to remove new lines and to be platform independent:
string.replace(os.linesep, "")
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
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")