Skip to content
UPEEC

Strings in Python

Text is stored in immutable str objects with powerful slicing, methods and f-string formatting.

Beginner
Comments 0
s = "UPEEC rocks"
s.lower()          # 'upeec rocks'
s.split()          # ['UPEEC', 'rocks']
s[0:5]             # 'UPEEC'  (slicing: start inclusive, end exclusive)
s[-5:]             # 'rocks'
f"{3.14159:.2f} V" # '3.14 V'  (f-string formatting)

Strings are immutable — methods return new strings. To build long strings efficiently, collect parts in a list and use "".join(parts).

bytes is a separate type for raw binary data, e.g. what you send over a serial port.

Spotted a mistake or want to add detail? Suggest an improvement — Content Contributors' edits are reviewed by editors before they go live.

Discussion (0)

Sign in or join free to ask questions and discuss this topic.