Coding Python
Home›Python Coding Games›String Surgeon

String Surgeon — Strings & slicing

Six operations on text, with a live index ruler so slicing finally stops being guesswork.

Level 1

Checks
    Your PythonPython 3
    1

    How to play

    Every level shows the string you are working on as a ruler: each character with its positive index above and its negative index below. Write the slice or method call that produces the piece the level asks for, press Run, and your results appear underneath with their types.

    The ruler exists because almost nobody learns slicing from a prose description of it. Seeing that "PYTHON"[2:4] covers exactly the two cells labelled 2 and 3 settles the question permanently.

    What each level teaches

    1. First, last, middle — indexing from both ends, and the fact that a slice excludes its stop value.
    2. Every other one — the third slice number, the step. s[::-1] reverses a string.
    3. Clean it up — .strip(), .split() and indexing into the result. This is the shape of nearly all real text processing.
    4. Build the sentence — f-strings, including format specs such as :.2f for money.
    5. Search and replace — .count(), .find() and .replace().
    6. Put it back together — join(), which is a method on the separator rather than on the list.

    The rule that explains most slicing confusion

    A slice s[a:b] includes a and excludes b. It looks arbitrary until you notice two things it buys you: the length of the slice is simply b - a, and s[:n] + s[n:] always reconstructs the original string with no overlap and no gap. Every missing-or-extra-character bug in slicing comes from forgetting this one rule.

    Strings never change

    Python strings are immutable. s.upper() does not modify s; it returns a new string and leaves the original alone. If you write s.upper() on a line by itself and then wonder why nothing happened, this is why — you have to store the result. The game catches this because the check reads your variable, not your intention. More on all of it in the Python strings tutorial.

    Practise strings & slicing on your phone — the Coding Python app runs real Python 3 on your phone, with examples, quizzes and challenges built in.
    Get it free

    Frequently asked questions

    How does string slicing work in Python?

    s[start:stop:step] takes characters from start up to but not including stop, moving by step. Any part can be left out: s[:3] is the first three characters, s[3:] is everything from index 3 onwards, and s[::-1] reverses the string.

    What does a negative index mean?

    It counts from the end: -1 is the last character, -2 the second to last. s[-3:] is therefore the last three characters. The ruler in this game shows both numbering systems at once.

    Why doesn't s.upper() change my string?

    Because strings are immutable. Every string method returns a new string and leaves the original untouched, so you must assign the result: s = s.upper().

    What is an f-string?

    A string prefixed with f, where anything inside braces is evaluated and inserted: f"{name} is {age}". It can also format — f"{price:.2f}" always shows two decimal places.

    Why is join() a method on the separator?

    Because it works with any iterable of strings, not just lists. Writing "-".join(words) keeps one implementation on the string type rather than duplicating it on every container type.

    Keep going

    ← All Python coding games  Read the tutorial →

    Take the games with you

    Coding Python packs a real Python 3 compiler, 30 example programs, 112 quiz questions and 12 self-checking challenges into one free Android app.

    Get it on Google Play
    Coding Python app icon