4

I want to join N strings together, one line for each item:

my_list=['one', 'two', 'three']
lines='\n'.join(my_list)

Unfortunately I need a trailing newline at the end of each line in lines. In the above solution the last line is missing the newline.

... I use Python 2.7

Timur Shtatland
  • 12,024
  • 2
  • 30
  • 47
guettli
  • 25,042
  • 81
  • 346
  • 663

4 Answers4

5

Just add another \n like this(since the output of join() is also a string):

my_list=['one', 'two', 'three']
print '\n'.join(my_list)+'\n'
riteshtch
  • 8,629
  • 4
  • 25
  • 38
5

In Python 3.6, you may also use generator expression (PEP289):

''.join(f"{row}\n" for row in my_list)

It's a bit cleaner to me.

Chuan Ma
  • 9,754
  • 2
  • 45
  • 37
  • It's not generator expressions that is new to python 3.6 but fstrings. See [PEP 498](https://docs.python.org/3/whatsnew/3.6.html#whatsnew36-pep498) – user8162 Sep 09 '21 at 09:32
1

Alternatively to manually adding to the return value of join, you could (manually as well) append the empty string to your list.

>>> my_list=['one', 'two', 'three']
>>> my_list.append('')
>>> '\n'.join(my_list)
'one\ntwo\nthree\n'
timgeb
  • 76,762
  • 20
  • 123
  • 145
  • 2
    There's no real reason to do this. It looks more efficient than `'\n'.join(my_list)+'\n'`, but due to how python cheats when an immutable object only has one reference, it's usually less efficient. The append way is about 25% slower on my machine *and*, it's not as obvious why the `''` is added compared to the `'\n'`. – Dunes Apr 15 '16 at 11:34
  • @Dunes Couldn't find evidence of that in the python source code. Could you point me to it? Thanks! – Bharel Sep 09 '18 at 12:18
  • 1
    @Bharel You can see direct evidence of this by doing `s= b'some-non-constant-string'.decode(); s = s + 'some string'`. Compare the value of `id(s)` before and after and note they are the same. If you initialised `s` with a string literal then the ids would be different. The implementation is across a parts of the code. The first place is `Objects/unicodeobject.c:PyUnicode_append` and `unicode_modifiable`. And the second is `Python/ceval.c:unicode_concatenate`, which is called if `s = s + t` or `s += t` is called. Without this the condition in `PyUnicode_Append` would never trigger. – Dunes Sep 09 '18 at 17:42
  • @Dunes amazing. Thank you very much! I was actually looking at PyUnicode_Concat and didn't realize there was another unicode concatenate. (Wondered who calls Append too) – Bharel Sep 09 '18 at 18:33
  • PyUnicode_Concat is the implementation of `str.__add__`. If you subclass-ed `str`, this is what would get called when you added your subclass instances together. PyUnicode_Append is the interpreter's micro-optimisation if it detects both operands to `+` have type `str`. – Dunes Sep 10 '18 at 10:22
  • @Dunes Should this be considered part of Python's promised behavior or an implementation detail of CPython? – Alan Jan 05 '21 at 12:12
0

Using print before, it works:

>>> print '\n'.join(ls)
one
two
three
>>> 

Check this answer for details on the difference on strings and string representation like mentioned here.

Here as well, you can find a solution.

rocksteady
  • 2,320
  • 5
  • 24
  • 40