Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructing a multiline string from array in python

I am currently working on a script which sends out the list of defaulters to a distro. I have the values in python list like this ['[email protected]','[email protected]','[email protected]'] I need to form a multiline string from the above array like this

""" 1. [email protected]
    2. [email protected]
    3. [email protected]"""

I tried this

defaultersList = ['[email protected]','[email protected]','[email protected]']  
multilineStr = ""

for item in defaultersList:
  multilineStr = multilineStr + '\n' + item

but out put doesnt come how I expected it to come. Its coming in single line with \n character

like image 775
Krish Avatar asked Jun 15 '26 22:06

Krish


1 Answers

You are looking for:

"\n".join(["{0}. {1}".format(i+1, person) for i, person in enumerate(defaultersList)])

Of course, the "\n" token will be in the string, but if you call print, you'll see a multiline string.

"\n" is how a newline is represented in Python (and computers in general).

like image 101
Thomas Orozco Avatar answered Jun 18 '26 10:06

Thomas Orozco



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!