Python, properly · Chapter 4 of 42

Strings

By the end of this chapter you can pick characters and pieces out of text, tidy messy text with string methods, and build exact lines of output with f-strings, prices and all. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: First steps
  • 4 tasks
  • Python
  • In the all labs plan

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.

Why you need this

Almost everything DabbaDrop knows arrives as text. An order's time is "2026-09-07 19:05", a customer types their email as " Ananya.PAWAR@Example.com", and Bhavna's order sheet writes the items as "roti:2;dal-tadka:1". Farah wants bills and kitchen slips in exactly the same shape every time.

So far you can only print text as it is. To be useful, a program has to take text apart, clean it, and put new text together. Python's strings have tools for all three.

What you build: 4 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    placed_at.py: date, time and day

    Every order has a placed_at time written as text. Start placed_at.py with this line, exactly:

    placed_at = "2026-09-14 13:41"

    Cut it into pieces with slicing and print them, so the output is exactly:

    Date: 2026-09-14

    Time: 13:41

    Day: 14

    The check changes placed_at to other times in the same format (like 2026-09-01 09:05, whose day is 01) and runs your file again.

  2. Task 2

    clean.py: tidy an email and a phone

    Customers type their details in every style. Start clean.py with these two lines, exactly:

    email = " Neel.DSouza@Example.com "

    phone = "83262-72722"

    Print the email with the spaces at both ends removed and in small letters, and the phone with every dash and every space removed:

    Email: neel.dsouza@example.com

    Phone: 8326272722

    The check changes both values (a phone like +91 84641 29694 becomes +918464129694) and runs your file again.

  3. Task 3

    bill_line.py: one line of the bill

    Farah wants every line of the bill in the same shape. Start bill_line.py with these three lines, exactly:

    dish = "Paneer Butter Masala"

    qty = 2

    price = 220

    With one f-string, print the quantity, the dish and the amount (quantity times price) with 2 decimal places and a comma between thousands:

    2 x Paneer Butter Masala = Rs 440.00

    The check changes the three values (12 Mutton Biryani at 340 must print Rs 4,080.00) and runs your file again.

  4. Task 4 · interview question

    Interview: mask.py, hide the phone number

    The rider's app may show only the last 4 characters of a customer's phone. Start mask.py with this line, exactly:

    phone = "8709470176"

    Print the phone with every character except the last 4 replaced by a star:

    Phone: ******0176

    The number of stars is the length of the phone minus 4, so the check's other phones (like +91 84641 29694, or 1234 with no stars at all) work too.

What you will learn

  • Indexing: one character by its position, counting from 0 or from the end.
  • Slicing: a piece of text, s[start:stop].
  • Methods: strip, lower, title, replace and friends, and why a string never changes.
  • f-strings: values inside text, with formats for prices.

More in First steps

Build "Strings" yourself

DabbaDrop, a Mumbai tiffin startup, runs on Excel and WhatsApp. You are its first Python developer. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Python, properly are free. This one comes with the all labs plan.