python string on one line
My string is
a
b
c
d
I want to do this
a b c d
开发者_开发问答
How?
Try
s = "a\nb\nc\nd\n"
t = str.join(" ", s.splitlines())
>>> txt="""a
b
c
d"""
>>> txt.replace("\n", " ")
'a b c d'
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.
精彩评论