Practice Exercises: Python String Methods#
These exercises were generated by ChatGPT using our course notebooks as input. They were briefly reviewed to make sure that they are correct and fit within the topics and level of the course, but they aren’t necessarily a comprehensive set of examples around the course topics.
1. Clean up messy district names#
You are given names with extra whitespace:
districts = [" Scarborough ", "North York", " Etobicoke ", "York "]
Create a new list clean_districts where each name has no leading or trailing spaces.
# Write your code here
districts = [" Scarborough ", "North York", " Etobicoke ", "York "]
2. Parse a simple CSV line (name + value)#
Each line is "district, population" but spacing is inconsistent:
line = "Scarborough, 632098 "
Extract:
district_nameas a clean stringpopulationas anint
# Write your code here
3. Build a dictionary from multiple CSV lines#
Given:
lines = [
"Scarborough, 632098",
"North York, 869401 ",
"Etobicoke, 365143",
]
Build a dictionary pop_by_district mapping district name → population (int). Use a for loop.
# Write your code here
4. Handle bad lines while parsing#
Now you may see missing or invalid values:
lines = [
"Scarborough, 632098",
"North York, ",
"Etobicoke, 365143",
"York, not_available"
]
Create a list valid_pops containing only the valid integer population values. Skip the bad ones.
There is a method called str.isdigit() that returns True if a string contains only digits.
# Write your code here
5. Replace inconsistent separators#
Some lines use semicolons instead of commas:
lines = [
"Scarborough;632098",
"North York,869401",
"Etobicoke; 365143"
]
Convert every line so it uses commas, then parse as before into a dictionary.
# Write your code here
6. Extract the domain from email addresses#
Given:
emails = ["ana@utoronto.ca", "bob@gmail.com", "c.chen@geography.org"]
Create a list domains containing only the part after the @.
# Write your code here
7. Turn “Last, First” into “First Last”#
Given:
names = ["Nguyen, Linh", "Smith, Jordan", "Patel, Asha"]
Create:
fixed = ["Linh Nguyen", "Jordan Smith", "Asha Patel"]
# Write your code here
8. Count categories in coded survey responses#
Survey responses are stored as strings:
responses = ["car", "Car ", " transit", "bike", "CAR", "transit", "walk "]
Make a dictionary counting each mode, treating different capitalizations and extra spaces as the same category.
# Write your code here
9. Find and clean unit numbers in addresses#
Addresses sometimes include apartment/unit info like this:
addresses = [
"12-100 King St W",
" 55 Queen St E",
"8-77 Bloor St W ",
]
Create two lists:
street_addresseswith the unit part removed (e.g.,"100 King St W")has_unitbooleans for whether there was a unit number
# Write your code here
10. Reconstruct a “slug” for filenames#
You want to turn titles into simple filenames:
titles = [
"Income Inequality in Toronto",
"Public Transit & Access",
"Housing Prices: 2015 to 2025"
]
Rules:
lowercase everything
replace spaces with underscores
remove colons
replace
&withand
titles = [
"Income Inequality in Toronto",
"Public Transit & Access",
"Housing Prices: 2015 to 2025"
]