Python - 加入换行符 [英] Python - Join with newline
本文介绍了Python - 加入换行符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 Python 控制台中,当我输入:
<预><代码>>>>"\n".join(['I', 'would', 'expect', 'multiple', 'lines'])给出:
'我\n会\n期望\n多行\n行'
虽然我希望看到这样的输出:
我将预计多线
我在这里遗漏了什么?
解决方案
控制台正在打印表示,而不是字符串本身.
如果你以print
为前缀,你会得到你所期望的.
请参阅这个问题,了解有关字符串之间差异的详细信息和字符串的表示.超级简化,表示就是您在源代码中键入的内容以获取该字符串.
In the Python console, when I type:
>>> "\n".join(['I', 'would', 'expect', 'multiple', 'lines'])
Gives:
'I\nwould\nexpect\nmultiple\nlines'
Though I'd expect to see such an output:
I
would
expect
multiple
lines
What am I missing here?
解决方案
The console is printing the representation, not the string itself.
If you prefix with print
, you'll get what you expect.
See this question for details about the difference between a string and the string's representation. Super-simplified, the representation is what you'd type in source code to get that string.
这篇关于Python - 加入换行符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文