loop
Working with strings

Module 2 · Working with strings · Lesson 2 of 4

String methods

Strings come with a rich toolkit of methods — functions you call on the string itself with a dot: text.upper(). Because strings are immutable, none of them changes the original; each one hands back a new string.

A method makes a new string
"hi".upper()"HI"
" hi ".strip()"hi"
"hi".replace("i","ey")"hey"

The everyday ones: upper()/lower() change case, strip() trims whitespace from the ends, and replace(old, new) swaps text. Since each returns a string, you can chain them.

Run it yourselfruns in your browser · Python
main.py

Two more that bridge strings and lists: split() breaks a string into a list of pieces (on spaces by default, or any separator you give), and join() does the reverse — gluing a list of strings back into one.

Run it yourselfruns in your browser · Python
main.py
✎ Your turnauto-checked · Python

Write a function normalise(name) that trims surrounding spaces and returns the name in all lower case. So normalise(' Ada ') returns 'ada'.

solution.py