String methods, explained simply
Text in Python comes with built-in tools called methods. You use one by writing a dot after the text (or a variable holding it), then the method name and brackets.
upper() makes every letter a capital, and lower() makes every letter small. strip() removes extra spaces from the start and end, which is handy when people type a space by accident. replace() swaps one piece of text for another.
Methods give back a new string. The original stays the same unless you store the result, for example word = word.strip().
Example: Tidy up a password
typed = " OpenSesame "
clean = typed.strip().lower()
print(clean)
print(clean.replace("sesame", "sky"))Output
opensesame
openskyreplace() found "sesame" inside the word and swapped it for "sky".
Try this
- Shout a message by printing it with upper().
- Ask for a yes or no answer and use lower() so "YES", "Yes" and "yes" all count.
- Use replace() to turn every "a" in a sentence into "@".
