String Methods: the Toolbox

Strings carry built-in tools called methods. You call them with a dot. The essential set:

Case: .upper() / .lower() (Python), .toUpperCase() / .toLowerCase() (JS)

Search: "ko" in word (Python), word.includes("ko") (JS)

Split: break a sentence into a list of words

Trim: strip stray spaces from the ends

EXAMPLE
loading editor…
YOUR TURN
Your turn → Add print(chant) at the end to confirm the original is unchanged. Then store the uppercase version back in chant and print it again.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

EXAMPLE
loading editor…
YOUR TURN
Your turn → Replace the sentence with a line from your favorite song, then run it to see the words and how many there are.
loading editor…

Syntax has to be exact. Every quote, bracket and parenthesis needs its partner, and in Python the spaces at the start of a line count. One slip and the program won't run; the message will say where.

✦ Immutability

Strings never change in place. Every method hands you a brand-new string and leaves the original untouched. If you want to keep the result, assign it: loud = chant.upper().

◈ Graded Challenge

The variable full_name holds "amara okafor". Create first_initial holding its first character, name_length holding how many characters it has, and shouted holding the whole name in capitals.

Name it exactly: first_initial, name_length, shouted

3 visible + 1 hidden
loading editor…

Mark this lesson complete

Signed in, your progress follows you to every device.

Next: Practice Test →
← Back