2

My string is

a
b
c
d

I want to do this

a b c d

How?

3
  • 1
    Can you elaborate the question? Commented Mar 17, 2011 at 17:42
  • Can you show us the exact code you're currently looking and what you want it to do? Commented Mar 17, 2011 at 17:42
  • 1
    You have a string with new lines and you want to replace them with spaces? Commented Mar 17, 2011 at 17:43

3 Answers 3

11

Try

s = "a\nb\nc\nd\n"
t = str.join(" ", s.splitlines())
Sign up to request clarification or add additional context in comments.

1 Comment

what is faster by the way? your variant or user's 'somthing' variant?
10
>>> txt="""a
b
c
d"""
>>> txt.replace("\n", " ")
'a b c d'

Comments

0

Now, if you are trying to make it one line for HTML in order to send it in an email or to past it in a webpage you can do as follow:

str.replace('\n', '<br/>').replace('\r', '<br/>')

But is this is only to make it HTML. I use it when when I'm using sendgrid API to pass email content in a get HTTP request.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.