How to append new data onto a new line

The Solution to How to append new data onto a new line is

If you want a newline, you have to write one explicitly. The usual way is like this:

hs.write(name + "\n")

This uses a backslash escape, \n, which Python converts to a newline character in string literals. It just concatenates your string, name, and that newline character into a bigger string, which gets written to the file.

It's also possible to use a multi-line string literal instead, which looks like this:


Or, you may want to use string formatting instead of concatenation:


All of this is explained in the Input and Output chapter in the tutorial.

~ Answered on 2014-02-17 21:43:49

Most Viewed Questions: