开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜