Python - 加入换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14560863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 11:50:09  来源:igfitidea点击:

Python - Join with newline

pythonstring

提问by TTT

In the Python console, when I type:

在 Python 控制台中,当我输入:

>>> "\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?

我在这里缺少什么?

采纳答案by unwind

The console is printing the representation, not the string itself.

控制台正在打印表示,而不是字符串本身。

If you prefix with print, you'll get what you expect.

如果您以 为前缀print,您将得到您所期望的。

See this questionfor 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.

有关字符串和字符串表示形式之间差异的详细信息,请参阅此问题。超级简化,表示就是您在源代码中键入以获取该字符串的内容。

回答by root

You have to print it:

你必须打印它:

In [22]: "\n".join(['I', 'would', 'expect', 'multiple', 'lines'])
Out[22]: 'I\nwould\nexpect\nmultiple\nlines'

In [23]: print "\n".join(['I', 'would', 'expect', 'multiple', 'lines'])
I
would
expect
multiple
lines

回答by Abhijit

You forgot to printthe result. What you get is the Pin RE(P)Land not the actual printed result.

你忘print了结果。你得到的是PRE(P)L,而不是实际的打印结果。

In Py2.x you should so something like

在 Py2.x 中,你应该像

>>> print "\n".join(['I', 'would', 'expect', 'multiple', 'lines'])
I
would
expect
multiple
lines

and in Py3.X, print is a function, so you should do

在 Py3.X 中,print 是一个函数,所以你应该这样做

print("\n".join(['I', 'would', 'expect', 'multiple', 'lines']))

Now that was the short answer. Your Python Interpreter, which is actually a REPL, always displays the representation of the string rather than the actual displayed output. Representation is what you would get with the reprstatement

这就是简短的回答。你的 Python 解释器,它实际上是一个 REPL,总是显示字符串的表示而不是实际显示的输出。repr陈述是你会得到的陈述

>>> print repr("\n".join(['I', 'would', 'expect', 'multiple', 'lines']))
'I\nwould\nexpect\nmultiple\nlines'

回答by Sibi

When you print it with this print 'I\nwould\nexpect\nmultiple\nlines'you would get:

当你用这个打印它时,print 'I\nwould\nexpect\nmultiple\nlines'你会得到:

I
would
expect
multiple
lines

The \nis a new line character specially used for marking END-OF-TEXT. It signifies the end of the line or text. This characteristics is shared by many languages like C, C++ etc.

\n是专门用于标记结束-OF-TEXT换行符。它表示行或文本的结束。许多语言(如 C、C++ 等)都具有这种特性。

回答by pradyunsg

You need to printto get that output.
You should do

你需要print得到那个输出。
你应该做

>>> x = "\n".join(['I', 'would', 'expect', 'multiple', 'lines'])
>>> x                   # this is the value, returned by the join() function
'I\nwould\nexpect\nmultiple\nlines'
>>> print x    # this prints your string (the type of output you want)
I
would
expect
multiple
lines