0

I was learning about files in python 3, making a .txt and trying to write things, and I wanted to write in line:

f=open("file.txt","w")
list=['hello',''how are you?']
for a in list:
        f.write(a)
f.close()

This is what I expected was:

Hello
How are you?

But the program write:

HelloHow are you?

How do I add a new line in the program?

2 Answers2

0

You need to add the new line special character.

So here it would look like:

f=open("file.txt","w")
list=['hello','how are you?']
for a in list:
        f.write(a + "\n")
f.close()
madamak
  • 18
  • 3
0

you can write to the file in this way.

file.write(your_string + '\n')
M Hamza Razzaq
  • 432
  • 2
  • 7
  • 15