CoursesAIMathCodePhysicsAbout and FAQ

TwiddleLab / Code / Build with Python

Text Tricks

Lesson 2 of 12 · 7 min · Free, no account needed

What you will learn

  • A method is a function that belongs to a value, called with a dot: msg.upper(), msg.lower(), msg.replace(old, new).
  • Strings never change. A method gives back a new string; keep it with msg = msg.upper().
  • Methods chain left to right: msg.lower() runs first, then .replace() works on its result.
  • line.split() cuts text into a list of words, the first job of any tokenizer.

Start the lesson

How the lesson goes

  1. You moderate a game chat. Blank every 'noob'.

    40 messages, any capitals. Tap each red word.

  2. What does this print?

    msg = "GG Noob", then msg.upper(), then print(msg).

  3. Push the string through the .upper() station.

    Drag it from the box into the station. Watch the box.

  4. Methods: functions that belong to a value.

    A method is called with a dot: msg.upper(), msg.lower(), msg.replace(old, new), msg.split().

    Strings are immutable: they never change. A method returns a new string, so msg above is still GG Noob.

    Methods chain: msg.lower() and then .replace(old, new) run left to right, each station working on the last one's result.

  5. Build your chat filter.

    Put stations on the belt so every 'noob', in any capitals, is blanked out.

  6. Drop each method call on what it gives.

    Five calls, five results.

  7. Where will .split() cut this line?

    Tap every bead where a cut happens, then press Split. Look closely: one gap is wider.

  8. Filter wars: players try to sneak past your filter.

    Run the stream. If something slips through, add a station and run again.

  9. This should print MIA. It prints mia.

    Tap the part that's wrong, pick a fix, then Run.

  10. Back to the chat from the start.

    Drag your filter onto it.

Previous lesson: Strings Are Beads · Next lesson: While: Until It's Done · Part of the course: Build with Python

Lessons are interactive and need JavaScript. Turn it on to play them.

Loading TwiddleLab