Beginner·7 min·basics · strings
Strings
Strings
Strings are immutable sequences of characters. Index with s[i], slice with s[a:b].
Slice syntax
s[start:stop:step] — any of the three can be omitted. s[::-1] reverses.
Common methods
.upper(),.lower(),.title().strip()— trim whitespace.replace(old, new).split(sep)andsep.join(list).startswith(),.endswith(),.find()
f-string formatting
f"{value:.2f}" — 2 decimal places. f"{n:>5}" — right-align in 5 chars.
Try it
- Take
" hello "and produce"Hello". - Reverse just the first word of
"python rocks".