text mode

The default way of opening a file, which decodes bytes into str using an encoding.

In more detail

In text mode read() returns strings, write() takes strings, and line endings are converted to a single newline. The encoding, usually UTF-8, turns bytes into characters, and bytes that are invalid in it raise UnicodeDecodeError. Add t to the mode or leave it out; text is the default.

Example

with open('notes.txt', encoding='utf-8') as f:
    text = f.read()   # str

Related terms

Taught in

References

Certification courses