18

Suppose my string is:

' Hai Hello\nGood eve\n'

How do I eliminate the '\n' in between and make a string print like :

 Hai Hello Good eve 

?

dnozay
  • 23,069
  • 5
  • 80
  • 100
user46646
  • 144,445
  • 43
  • 74
  • 84
  • 11
    He is NOT asking something to the effect of what is the function to replace string. This cannot be directly looked up in the manual. If everybody could look up the manual, there would not be stackoverflow! – Niyaz Feb 04 '09 at 12:58
  • 2
    @Niyaz: If the question is not asking for string.replace(), what IS this person asking for? Feel free to show what is REALLY being asked by providing an answer. – S.Lott Feb 04 '09 at 13:02
  • 1
    From looking at the layout of the question, I think the OP wants to know how to render a string with newlines, but didn't read the formatting guidelines... and used several question marks. – Torsten Marek Feb 04 '09 at 13:10
  • 5
    Still, let us be nice to noobs. – Niyaz Feb 04 '09 at 13:12
  • 3
    Give the guy a break. If you think the question is dumb or obvious then don't answer it and move on with your life. Geez. – Justin Bennett Feb 04 '09 at 13:15
  • From the title I thought this was about sys.stdout.write() style answers, which is very hard to get from the manual. string replace() should however have been discoverable from the manual. – Douglas Leeder Feb 04 '09 at 13:15
  • 1
    @Niyaz, agreed, but messed-up formatting is IMHO impolite. As we don't see the person behind the question, using appropriate formatting (I don't even require good English skills) is a minimum of politeness we can expect. – Torsten Marek Feb 04 '09 at 13:17
  • 1
    Click on the profile for rejinacm. There are at least 30 of the 34 questions which are trivially solved by opening the Python doco pages. This is moving beyond a n00b thing into a "plzsendtehcodez" thing. The first dozen questions gave the n00b a break. – S.Lott Feb 04 '09 at 14:02
  • 1
    While coding i used get such doubts.As i have limited time i searched in the web and i found a good site like stackoverflow. I got so many useful answers and so i saved my time .After my office time i goes through the manual.It is due to time limitation. Dont be so bad to anybody. – user46646 Feb 04 '09 at 14:19
  • @rejinacm: (1) we all have time limitations. (2) asking is the slowest way to learn. – S.Lott Feb 04 '09 at 14:48
  • @rejinacm: SO is not an interactive tutorial with question-answering bots. – Torsten Marek Feb 04 '09 at 15:32
  • 3
    @S: your time limitations are not apparent to me ;) – Dustin Getz Mar 27 '09 at 16:35
  • 1
    We were all new at one time, just politely say "the answer is XYZ and I found it from here" – Teifion Mar 27 '09 at 16:52
  • he's not asking for the replace method. he wants to have string with several lines without explicitly putting \n for every line. – hasen May 02 '09 at 08:13
  • I think this 15 comment waste-of-time thread proves that none of you have time limitations :) – Ali Gangji Nov 12 '12 at 01:43

8 Answers8

23

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

import sys
sys.stdout.write("text")
Staale
  • 25,954
  • 23
  • 64
  • 85
  • I felt the information was related to the question at hand. And there where already good answers. – Staale Feb 04 '09 at 13:03
  • 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 Feb 04 '09 at 13:13
  • 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 Feb 04 '09 at 13:17
  • 1
    To Justin Bennett with greetings from germany: "YOU don't know what 'bunch of Nazis' mean !" – Blauohr Feb 04 '09 at 13:31
  • 2
    The question changed. Please delete all of these comments. – S.Lott Feb 04 '09 at 14:46
21

You can use the replace method:

>>> a = "1\n2"
>>> print a
1
2
>>> a = a.replace("\n", " ")
>>> print a
1 2
Torsten Marek
  • 79,210
  • 20
  • 90
  • 98
11

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
  • 1,914
  • 13
  • 14
4

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

some more text

Instead of just printing:

"some text\nsome more text"

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:

"some text\nsome more text"

is in fact

"some text\\nsome more text"

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.

Matt Olan
  • 1,781
  • 17
  • 25
manaman19
  • 41
  • 1
3

Add a comma after "print":

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

Altho "print" is gone in Python 3.0

chacmool
  • 1,265
  • 2
  • 15
  • 20
2

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)

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...,"")

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).

divot
  • 21
  • 3
1
>>> 'Hai Hello\nGood eve\n'.replace('\n', ' ')
'Hai Hello Good eve '
DzinX
  • 51,694
  • 9
  • 59
  • 78
0

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
  • 155,371
  • 64
  • 187
  • 227