Chapter 0 · Section 2 practice
Strings and Clear Output
Work from question 1 to 10. Write your code, click Run Code, review the result, then click Submit attempt. An attempt may be wrong: reference answers become available after you submit all 10. Each Python box is independent. Submission progress is saved in this browser.
1. Create a string
Warm-up. Store your example name Alex in a variable and print it. Print its type on a second line.
Write your answer, then run it.
2. Join two names
Warm-up. Join first_name and last_name with a space between them. Print the full name.
Write your answer, then run it.
3. Repeat text
Warm-up. Use string multiplication to print Go! three times, with spaces between the words.
Write your answer, then run it.
4. A birthday message
Build your skills. Using an f-string, print Next year Alex will be 17. Calculate the next age inside the braces.
Write your answer, then run it.
5. Display a price
Build your skills. Print Price: HK$8.50 using an f-string and two decimal places. Then print the stored price itself.
Write your answer, then run it.
6. Numeric text
Build your skills. Convert the text values to integers and print their numeric sum. Do not join the text.
Write your answer, then run it.
7. A bill from form inputs
Build your skills. Convert the price text to float and quantity text to int. Print Bill: HK$37.50.
Write your answer, then run it.
8. Clean a label
Challenge. Remove the surrounding spaces and convert the label to uppercase. Print the clean label between square brackets so extra spaces are visible.
Write your answer, then run it.
9. Keep an identifier as text
Challenge. The student identifier is 0042. Keep its leading zeros. Print Student: 0042; on a second line show what int(student_id) would produce.
Write your answer, then run it.
10. A complete receipt
Challenge. Clean the item name, convert the numeric text, and print PEN: 3 items, HK$13.50 using an f-string.
Write your answer, then run it.
Reference answers are locked until all 10 attempts are submitted.
1. Create a string
name = "Alex"
print(name)
print(type(name))Expected output
Alex
<class 'str'>A name inside quotes is text, of type str.
2. Join two names
first_name = "Alex"
last_name = "Chan"
full_name = first_name + " " + last_name
print(full_name)Expected output
Alex ChanConcatenation does not insert a space automatically; include the space as text.
3. Repeat text
print("Go! " * 3)Expected output
Go! Go! Go! The repeated string includes a space. A trailing space is harmless here.
4. A birthday message
name = "Alex"
age = 16
print(f"Next year {name} will be {age + 1}.")Expected output
Next year Alex will be 17.The f prefix evaluates the expressions inside braces.
5. Display a price
price = 8.5
print(f"Price: HK${price:.2f}")
print(price)Expected output
Price: HK$8.50
8.5Formatting produces a label without changing the stored number.
6. Numeric text
first_text = "20"
second_text = "5"
print(int(first_text) + int(second_text))Expected output
25Without conversion, joining these strings would give 205.
7. A bill from form inputs
price_text = "12.50"
quantity_text = "3"
price = float(price_text)
quantity = int(quantity_text)
subtotal = price * quantity
print(f"Bill: HK${subtotal:.2f}")Expected output
Bill: HK$37.50Choose conversions that match a decimal price and a whole-number count.
8. Clean a label
raw_label = " school cafe "
clean_label = raw_label.strip().upper()
print(f"[{clean_label}]")Expected output
[SCHOOL CAFE]strip and upper return new strings; the brackets make surrounding spaces easy to check.
9. Keep an identifier as text
student_id = "0042"
print(f"Student: {student_id}")
print(int(student_id))Expected output
Student: 0042
42An identifier is often text. Numeric conversion loses its display-leading zeros.
10. A complete receipt
item_text = " pen "
quantity_text = "3"
price_text = "4.50"
item = item_text.strip().upper()
quantity = int(quantity_text)
price = float(price_text)
bill = quantity * price
print(f"{item}: {quantity} items, HK${bill:.2f}")Expected output
PEN: 3 items, HK$13.50Text cleaning, numeric conversion, calculation, and formatting serve different purposes.