Python Tutorial · Revision notes
Python Strings
A string is an ordered, immutable sequence of characters used to store text. This chapter covers creating strings, the most useful string methods, formatting with f-strings and slicing and indexing.
Quick revision
- str is an ordered, immutable sequence of Unicode characters.
- Methods return new strings: upper, lower, strip, replace, split, join, find, count, startswith.
- find() gives -1 if missing; index() raises ValueError.
- f"{value:.2f}" rounds; {n:,} adds commas; {x:>8} aligns; {x=} debugs.
- Indexing starts at 0; negative index starts at -1 from the end.
- s[start:stop:step]; stop is excluded; s[::-1] reverses; slices never raise IndexError.
- Use "".join(list) instead of += in loops for speed.