Python Tutorial: String Methods
Slice, search, split, join, and transform text. Strings are everywhere — master them.
What you'll learn in this Python string methods tutorial
This interactive Python tutorial has 9 hands-on exercises. Estimated time: 16 minutes.
- Strings are arrays of characters — Every string in Python is a sequence. You can grab any character by its position (index), starting at 0.
- Slicing — grab a chunk — Slicing lets you extract a piece of a string: `text[start:end]`.
- Useful string methods — Python strings have dozens of built-in methods. The most common:
- Find and count — `.find(text)` returns the index where a substring first appears (-1 if not found).
- Split — break a string apart — `.split(separator)` breaks a string into a list of pieces.
- Join — stitch a list back together — `.join(list)` is the opposite of split — it glues a list of strings into one string.
- Replace — `.replace(old, new)` swaps every occurrence of `old` with `new`.
- The `in` operator — The simplest way to check if text contains something: the `in` keyword.
- Build a text cleaner 🔧 — Build a function `clean_username(raw)` that: