A
A
Asya2020-06-14 11:00:37
Python
Asya, 2020-06-14 11:00:37

How to save a line to a file with line breaks?

I have a line when I do it , it is displayed when I try to write to a filemystr="here\n is\n my\n string\n"

print(mystr)
here\n is\n my\n string\n

print(mystr, file=(open('myfile.txt', 'w', newline='\n')), sep='\\n')
then the file is also simple, here\n is\n my\n string\n
but you need it to be:
here
 is
 my
 string

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
soremix, 2020-06-14
@asyaevloeva

Of course, you have an interesting type of writing to a file

mystr="here\n is\n my\n string\n"

with open('myfile.txt', 'w') as f:
    f.write(mystr)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question