Python - Join with newline

PythonString

Python Problem Overview


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?

Python Solutions


Solution 1 - Python

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.

Solution 2 - Python

You forgot to print the result. What you get is the P in RE(P)L and not the actual printed result.

In Py2.x you should so something like

>>> 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

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 repr statement

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

Solution 3 - Python

You need to print to get that output.
You should do

>>> 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

Solution 4 - Python

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

Solution 5 - Python

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

I
would
expect
multiple
lines

The \n is 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.

Solution 6 - Python

The repr() function returns a printable representation of the given object and is crucial for evalStr() or exec in Python; for example you want to get out the Zen of Python:

eng.execString('from this import *');
println('import this:'+CRLF+
  stringReplace(eng.EvalStr('repr("".join([d.get(c,c) for c in s]))'),'\n',CRLF,[rfReplaceAll]));

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionTTTView Question on Stackoverflow
Solution 1 - PythonunwindView Answer on Stackoverflow
Solution 2 - PythonAbhijitView Answer on Stackoverflow
Solution 3 - PythonpradyunsgView Answer on Stackoverflow
Solution 4 - PythonrootView Answer on Stackoverflow
Solution 5 - PythonSibiView Answer on Stackoverflow
Solution 6 - PythonMax KleinerView Answer on Stackoverflow