Skip to main content

Strings and Unicode Text

Python str represents immutable Unicode text. bytes represents encoded binary data. Crossing between them requires an explicit encoding.

text = "café"
payload = text.encode("utf-8")
restored = payload.decode("utf-8")

Keep text as str inside the program and encode/decode at file, network, or subprocess boundaries.

Sequence operations

name = "Ada Lovelace"
first = name[:3]
last_character = name[-1]
contains_space = " " in name

Indexes and slices operate on Unicode code points, not user-perceived grapheme clusters. Some visible characters consist of multiple code points.

Because strings are immutable, transformations create new strings:

normalized = raw.strip().casefold()
words = normalized.split()
line = ", ".join(words)

Repeated += in a large loop can create avoidable intermediate strings; collect pieces and use join when construction cost matters.

Formatting

Use f-strings for local readable interpolation and format specifications:

message = f"{user}: {total:,.2f} CAD"

Formatting is not escaping. SQL, HTML, shell commands, regular expressions, and URLs each require context-specific APIs; never rely on an f-string for safety.

Unicode equality

Visually similar text can use different code-point sequences. Normalize when a domain requires canonical comparison, and use casefold rather than lower for aggressive caseless matching. Locale-aware collation and human name handling need domain-specific libraries and rules.

Source