Essential string methods in Python
The everyday toolkit for cleaning and probing text.
raw = " The Quick Brown Fox "
clean = raw.strip()
shout = clean.upper()
quiet = clean.lower()
title = quiet.title()
swapped = clean.replace("Quick", "Slow")
where = clean.find("Brown")
starts = clean.startswith("The")
ends = clean.endswith("Fox")
count_o = clean.count("o")
How it works
stripremoves surrounding whitespace.- Case changes with
upper,lower, andtitle. replaceswaps substrings;findlocates one.startswith,endswith, andcountanswer questions.
The run, in numbers
- Lines
- 13
- Characters to type
- 278
- Tokens
- 87
- Three-star pace
- 85 tpm
At the three-star pace of 85 tokens a minute, this run takes about 61 seconds.
Step 2 of 9 in Strings, step 8 of 72 in Language basics.