🐍 Python for Everyone

← Back to the quiz

Chapter 6 Exercises 20 min, hands-on

Exercises: Strings Deep Dive

How to run these Download starter.py below, then run python3 starter.py.

1 File extension extractor

Given filename = "vacation_photo.jpeg", use slicing to extract just the file extension (".jpeg") and print it.

2 Email match checker

Given email1 = " Bob@Example.COM " and email2 = "bob@example.com", normalize both (strip whitespace, lowercase) and print whether they match.

3 Receipt line with an f-string

Given item = "Desk Lamp", price = 18.5, and qty = 3, use an f-string to print a receipt line in the form: "Desk Lamp x3 = $55.50".

4 Username validator

Given username = "sky walker 99", check whether it contains a space. Print "Invalid username: no spaces allowed." if it does, or "Username OK." if it doesn't.

🐞 Task 5 — Debug the Code

This is supposed to print "HELLO", but it prints "hello" instead. Find and fix the bug.

buggy.py
greeting = "hello"
greeting.upper()
print(greeting)

6 Word tally

Given text = "to be or not to be that is the question" and target = "to", count how many times target appears in text (using .split() and an accumulator), then print the result in the form: "to" appeared 2 times.

← Back to Quiz Practice Bank →