3

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 ['person1@email.com','person2@email.com','person3@email.com'] I need to form a multiline string from the above array like this

""" 1. person1@email.com
    2. person2@email.com
    3. person3@email.com"""

I tried this

defaultersList = ['person1@email.com','person2@email.com','person3@email.com']  
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

Krish
  • 467
  • 1
  • 6
  • 16

1 Answers1

1

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

Thomas Orozco
  • 53,284
  • 11
  • 113
  • 116
  • I am not printing the string instead it will be email body like this msg = MIMEText("""this is the body of the email""") ... sure \n ill give a new line? – Krish Sep 11 '13 at 20:57
  • Alternative suggestion: `"".join(starmap("{0}. {1}\n".format, enumerate(defaultersList, 1)))` (with `starmap()` from `itertools`). – Sven Marnach Sep 11 '13 at 20:58
  • I am not printing the string instead it will be email body like this msg = MIMEText("""this is the body of the email""") ... sure \n ill give a new line? – Krish Sep 11 '13 at 21:01