Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Join with newline

Tags:

python

string

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?

like image 542
TTT Avatar asked Jan 28 '13 11:01

TTT


People also ask

How do you join a newline in Python?

To join a list with a newline character in Python: Call the join() method on a string containing a newline char ( '\n' ). Pass the list to the join method. The result will be a string containing the list items separated by a newline.

Does \n work in a string?

in a string prevents actually making a new line and instead of Type (new line) for a new line it is Type \n for a new line .

How do I check if a string contains a newline in Python?

Check if a string contains newlines using the 'in' operator With the 'in' operator, we can check for a specified value in a string. However, this method returns True if the value exists. Otherwise, returns False.

How do you add a line break in string?

In Windows, a new line is denoted using “\r\n”, sometimes called a Carriage Return and Line Feed, or CRLF. Adding a new line in Java is as simple as including “\n” , “\r”, or “\r\n” at the end of our string.


4 Answers

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.

like image 92
unwind Avatar answered Oct 06 '22 05:10

unwind


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' 
like image 39
Abhijit Avatar answered Oct 06 '22 06:10

Abhijit


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
like image 20
pradyunsg Avatar answered Oct 06 '22 07:10

pradyunsg


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
like image 40
root Avatar answered Oct 06 '22 06:10

root