Concepts / Reading and Writing Text Files

Reading and Writing Text Files

When text appears on two lines, it is tempting to think the line break is only a visual feature added by the screen.

  • Programming

The Invisible Boundary

When text appears on two lines, it is tempting to think the line break is only a visual feature added by the screen. In a Python string or text file, however, the line break is part of the data. Python represents this character with the escape sequence \n. Although the source code shows a backslash and the letter n, \n represents one invisible character that marks the end of a line.

\source symbolnewline characterone invisible characternsource symbolwithwith
How do the visible symbols backslash and n in source code correspond to a single character in the actual string?

Two Views of One String

Python can show the same newline character in two different ways. When you enter a variable name in the Python interpreter, the interpreter shows the string's representation. In that view, the newline appears as the visible escape sequence \n. When you pass the same string to print(), Python interprets the newline character as a line break, so the text after it appears on the next line. A text editor similarly displays the newline as an actual separation between lines.

interpreterHello\nWorld!print()Hello, then World!line breakscreen moves to next linesame stringinterprets \n
Why does the interpreter show \n while print() moves the following text onto a new line?
Worked example

Reading the Same Data in Two Ways

Consider the string Hello\nWorld!. What should you expect to see when Python shows its representation, and what should you expect when print() displays it?

  1. 1
    Interpreter view

    The interpreter displays the newline using escape sequence notation, so the visible representation contains Hello\nWorld!.

  2. 2
    Printed view

    print() interprets the newline character as a line break. Hello appears on one line and World! appears on the next.

  3. 3
    Meaning

    The stored string has not changed between these views. Only the way the newline is represented on the screen has changed.

Result

The interpreter shows the escape sequence, while print() displays the line break.

Counting Every Character

A newline character occupies a position in a string just like a letter does. In the string Hello\nWorld, the letters in Hello use indexes 0 through 4. The newline is at index 5, immediately after the o. The letters in World then use indexes 6 through 10. Therefore, the string contains 11 characters. The two visible symbols used to write \n do not count as two characters in the resulting string; the newline they represent counts as one.

0H1e2l3l4o5newline character6W7o8r9l10d
Which positions and characters contribute to the length of a string containing one newline character?

What do you think happens?

How many characters are in Hello\nWorld?

  • 10

  • 11

  • 12

Reveal answer
Answer:

11

Hello has 5 characters, the newline has 1 character, and World has 5 characters. The total is 5 + 1 + 5 = 11.

Newlines Inside File Data

A text file can be understood as text data containing newline characters. Each newline marks the boundary between one line and the next. When Python reads a file line by line, it uses these newline characters to determine where a line ends. The newline is therefore not merely decoration added during display; it is real data that must be accounted for when reading, comparing, slicing, or counting file contents.

Helloline dataline 1Hellonewlineline boundaryline 2WorldWorldline dataends withseparates fromformsforms
Where are newline characters located in file data, and how do they divide one continuous string into separate lines?
In the real world

Suppose a value read from a file appears to contain a word, but a comparison with another string unexpectedly fails. Or suppose output contains an extra blank line. The newline character at the line boundary may still be part of the data. Treating it as a real character helps you investigate the actual contents instead of relying only on how the text looks on screen.

Mistakes with Newlines

  • Common mistake

    Counting the two visible symbols in \n as two characters.

    What it looks like

    Treating Hello\nWorld as though it contains 12 characters.

    Why it fails

    The escape sequence represents one newline character in the resulting string.

    Do this instead

    Count the newline as one character. Hello\nWorld contains 11 characters.

  • Common mistake

    Assuming the interpreter's displayed \n is literal text stored instead of a representation of a newline.

    What it looks like

    Expecting the interpreter display and print() output to look identical.

    Why it fails

    The interpreter shows a representation using escape sequence notation, while print() interprets the newline as a line break.

    Do this instead

    Distinguish the representation of the string from the way the string is rendered.

  • Common mistake

    Ignoring newline characters when inspecting file data.

    What it looks like

    Treating a line read from a file as containing only its visible letters.

    Why it fails

    Newline characters are real data at line boundaries.

    Do this instead

    Account for the newline when reasoning about indexes, lengths, comparisons, and extra blank lines.

Check Your Understanding

EASY

A text value contains the characters A, B, a newline, and C. State the index of the newline and the total string length. Then describe how the interpreter representation and print() display would differ.

Hints
  • Assign indexes from left to right, starting at 0.
  • Count the newline as one character.
  • The interpreter uses escape sequence notation, while print() renders a line break.
Worked example

Checking the Practice Answer

Analyze the text value AB\nC.

  1. 1
    Index the characters

    A is at index 0, B is at index 1, the newline is at index 2, and C is at index 3.

  2. 2
    Count the characters

    There are four characters total because the newline contributes one character.

  3. 3
    Compare displays

    The interpreter representation shows AB\nC, while print() displays AB and C on separate lines.

Result

The newline is at index 2, the length is 4, and the two displays differ because they represent the same newline differently.

Key Takeaways

  1. 1
    \n is written as two visible symbols but represents one invisible newline character.
  2. 2
    The Python interpreter displays the newline with escape sequence notation, while print() and text editors show an actual line break.
  3. 3
    A newline occupies one index and counts as one character when calculating string length.
  4. 4
    Text files use newline characters to mark line boundaries, so file data can contain newlines that are not obvious on screen.
  5. 5
    Remembering the difference between raw data and rendered text helps explain unexpected lengths, comparisons, slices, and extra blank lines.

At a glance

  • \n represents one invisible character, not two separate characters.
  • The interpreter shows \n as a representation, while print() displays the corresponding line break.
  • Newline characters count toward string length and occupy their own indexes.
  • Newline characters in text files separate one line from the next and are part of the file data.

Related concepts

More concepts starting with R