Also known as: str, text
The str type in Python represents an immutable sequence of Unicode characters. Strings can be created with single quotes ('hello'), double quotes ("hello"), or triple quotes ('''multi-line''' or """multi-line"""). Since Python 3, all strings are Unicode by default, meaning they can hold characters from every writing system — Latin, Cyrillic, Chinese, Arabic, emoji — without special encoding declarations.
Strings support a vast collection of methods: .upper(), .lower(), .strip(), .split(), .join(), .replace(), .startswith(), .endswith(), .find(), .count(), and many more. They also support slicing (s[1:5]), membership testing ('x' in s), and iteration. Because strings are immutable, every method that appears to modify a string actually returns a new string object.
F-strings (formatted string literals, introduced in Python 3.6) are the modern way to embed expressions inside strings: f"The answer is {2 + 2}". They are readable, fast, and support format specifications for alignment, padding, and precision. Earlier formatting approaches — the % operator and .format() method — still work but are less idiomatic. Strings are encoded to and decoded from bytes using codecs like UTF-8, which is the dominant encoding on the modern web.
Related terms: F-string, Immutable, Regular Expression
Discussed in:
- Chapter 3: Variables and Data Types — Strings
- Chapter 7: Strings and Text Processing — String Basics