Practice by topic / Python & code / Python

Python Strings Exercises

Python is the most popular language for automating work: cleaning data, renaming files, crunching numbers. Each exercise runs your real code in the browser and checks it. These 5 exercises build up step by step, starting with tidy a name, get the email domain, check a prefix.

3 of 5 free · no sign-up needed · instant feedback

1

Tidy a Name

Free

<code>raw</code> holds a messy customer name. Create <code>clean</code>: spaces trimmed from both ends and each word capitalised (<code>Tan Wei Ling</code>).

Show the solution

Chain two methods: clean = raw.strip().title()

Exactly — .strip() then .title() is the Python version of TRIM + PROPER in Excel.

Try it in the live editor →
2

Get the Email Domain

Free

Create <code>domain</code>: everything after the <code>@</code> in <code>email</code>.

Show the solution

domain = email.split("@")[1]

Right — split('@') breaks the text at the @, and [1] takes the part after it.

Try it in the live editor →
3

Check a Prefix

Free

Create <code>is_invoice</code>: <code>True</code> if <code>code</code> starts with <code>INV-</code>, otherwise <code>False</code>.

Show the solution

is_invoice = code.startswith("INV-")

Good — .startswith() returns True or False directly, no if statement needed.

Try it in the live editor →
5

Pad an Order Number

Create <code>order_code</code>: <code>ORD-</code> followed by <code>n</code> padded with zeros to 5 digits (7 becomes <code>ORD-00007</code>).

Solution and instant grading: open it in the practice app (use one of your 20 free picks, or Pro).

Try it in the live editor →