如果我的输入文字是
a
b
c
d
e
f
g
Run Code Online (Sandbox Code Playgroud)
我希望我的输出文本是:(带双引号)
"a b c d e f g"
Run Code Online (Sandbox Code Playgroud)
这一步后我该去哪里:
" ".join([a.strip() for a in b.split("\n") if a])
Run Code Online (Sandbox Code Playgroud)
您已成功构造了不带引号的字符串.所以你需要添加双引号.在Python中有几种不同的方法可以做到这一点:
>>> my_str = " ".join([a.strip() for a in b.split("\n") if a])
>>> print '"' + my_str + '"' #Use single quotes to surround the double quotes
"a b c d e f g"
>>> print "\"" + my_str + "\"" #Escape the double quotes
"a b c d e f g"
>>> print '"%s"'%my_str #Use string formatting
"a b c d e f g"
Run Code Online (Sandbox Code Playgroud)
任何这些选项都是有效且惯用的Python.我可能会自己选择第一个选项,因为它简短明了
| 归档时间: |
|
| 查看次数: |
25542 次 |
| 最近记录: |