-1

In the code

list1=['my data items1', 'my data items2','my data items3']

When I do

str1=''
for i in list1:
    str1=str1+i+'\n'

I get str1='my data items1\nmy data items2\nmy data items3'

I want str1 to be a multiline string but it seems there is no way to do that without using print statement or is there a way?

I expected a multiline string but not getting one without using print. I need to send this list as email body so need a variable assigned to a string variable.

Vinayak s
  • 1
  • 1
  • Where is this "multiline" string going to be used? What does "_without print_" mean? Saying "multiline" usually means you need to _show or display_ the string _somewhere_, so typical examples add a `\n` then print it out to the console or write it to a file. – Gino Mempin Apr 22 '23 at 03:09
  • Seems like a duplicate of [Make a list of strings into a string with each value on a new line](https://stackoverflow.com/q/45982879/2745495), but just not `print`-ing out the resulting string. – Gino Mempin Apr 22 '23 at 03:14
  • see: https://stackoverflow.com/a/4601716/2681662 – MSH Apr 23 '23 at 13:12
  • I need to send it as an email body so using print is not an option. – Vinayak s Apr 24 '23 at 13:29

2 Answers2

0

You could just use string join() here:

list1 = ['my data items1', 'my data items2','my data items3']
output = "\n".join(list1)
print(output)

This prints:

my data items1
my data items2
my data items3
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
-1
list1 = ['my data items1', 'my data items2', 'my data items3']
str1 = ""
for i in list1:
    str1 += i + '\n'

multiline_str = str1

print(multiline_str)