Categories
Python Answers

How to specify new lines on Python, when writing on files?

Spread the love

Sometimes, we want to specify new lines on Python, when writing on files.

In this article, we’ll look at how to specify new lines on Python, when writing on files.

How to specify new lines on Python, when writing on files?

To specify new lines on Python, when writing on files, we can write the new line characters separately.

For instance, we write

file.write(line1)
file.write("\n")
file.write(line2)
file.write("\n")
file.write(line3)
file.write("\n")

to call file.write with the line1, line2, and line3 strings separated by newline characters.

file is the file opened with open with write permissions.

Conclusion

To specify new lines on Python, when writing on files, we can write the new line characters separately.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *