Fix Python – Python – Join with newline

Question

Asked By – TTT

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?

Now we will see solution for issue: Python – Join with newline


Answer

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.

This question is answered By – unwind

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0