Solutions: Practice Exercises — Python String Methods (Novice-Friendly)#
These solutions avoid list comprehensions and exceptions, and use clear step-by-step code. Each solution includes one or two short comments explaining what’s happening.
1. Clean up messy district names#
districts = [" Scarborough ", "North York", " Etobicoke ", "York "]
clean_districts = []
for d in districts:
# strip() removes spaces (and other whitespace) from the start and end
cleaned = d.strip()
clean_districts.append(cleaned)
clean_districts
2. Parse a simple CSV line (name + value)#
line = "Scarborough, 632098 "
# split into two parts around the comma
parts = line.split(",")
district_name = parts[0].strip() # clean up extra spaces
pop_text = parts[1].strip()
population = int(pop_text) # convert digits to an integer
district_name, population
3. Build a dictionary from multiple CSV lines#
lines = [
"Scarborough, 632098",
"North York, 869401 ",
"Etobicoke, 365143",
]
pop_by_district = {}
for line in lines:
# split into name and population (at the first comma)
name_text, pop_text = line.split(",", 1)
name = name_text.strip()
pop = int(pop_text.strip())
# store the result in the dictionary
pop_by_district[name] = pop
pop_by_district
4. Handle bad lines while parsing (no exceptions)#
lines = [
"Scarborough, 632098",
"North York, ",
"Etobicoke, 365143",
"York, not_available"
]
valid_pops = []
for line in lines:
parts = line.split(",", 1)
value_text = parts[1].strip()
# only keep the value if it is made of digits (e.g., "365143")
if value_text.isdigit():
valid_pops.append(int(value_text))
valid_pops
5. Replace inconsistent separators#
lines = [
"Scarborough;632098",
"North York,869401",
"Etobicoke; 365143"
]
pop_by_district = {}
for line in lines:
# replace semicolons with commas so we can split consistently
normalized = line.replace(";", ",")
name_text, pop_text = normalized.split(",", 1)
name = name_text.strip()
pop = int(pop_text.strip())
pop_by_district[name] = pop
pop_by_district
6. Extract the domain from email addresses#
emails = ["ana@utoronto.ca", "bob@gmail.com", "c.chen@geography.org"]
domains = []
for e in emails:
# split on '@' and take the part after it
parts = e.split("@", 1)
domain = parts[1]
domains.append(domain)
domains
7. Turn “Last, First” into “First Last”#
names = ["Nguyen, Linh", "Smith, Jordan", "Patel, Asha"]
fixed = []
for n in names:
# split into last and first around the comma
last_text, first_text = n.split(",", 1)
last = last_text.strip()
first = first_text.strip()
fixed.append(first + " " + last)
fixed
8. Count categories in coded survey responses#
responses = ["car", "Car ", " transit", "bike", "CAR", "transit", "walk "]
counts = {}
for r in responses:
# normalize by trimming spaces and making lowercase
key = r.strip().lower()
# update the count (initialize to 0 if key not seen yet)
if key in counts:
counts[key] = counts[key] + 1
else:
counts[key] = 1
counts
9. Find and clean unit numbers in addresses#
addresses = [
"12-100 King St W",
" 55 Queen St E",
"8-77 Bloor St W ",
]
street_addresses = []
has_unit = []
for addr in addresses:
s = addr.strip() # clean up whitespace first
# if there's a dash, treat the part before the dash as a unit number
if "-" in s:
unit_part, street_part = s.split("-", 1)
street_addresses.append(street_part.strip())
has_unit.append(True)
else:
street_addresses.append(s)
has_unit.append(False)
street_addresses, has_unit
10. Reconstruct a “slug” for filenames#
titles = [
"Income Inequality in Toronto",
"Public Transit & Access",
"Housing Prices: 2015 to 2025"
]
def make_slug(title):
# lower() makes everything lowercase
s = title.lower()
# remove colons and replace '&' with 'and'
s = s.replace(":", "")
s = s.replace("&", "and")
# split() breaks on any whitespace; join back with underscores
words = s.split()
slug = "_".join(words)
return slug
slugs = []
for t in titles:
slugs.append(make_slug(t))
slugs