开发者

python string on one line

开发者 https://www.devze.com 2023-02-17 06:46 出处:网络
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())

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

精彩评论

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