← All Python interview questions
Clean messy city names from a CRM export
EasyStrings & Text CleaningZomatoRazorpay
A CRM export has arrived with inconsistent city names — stray spaces, SHOUTING CAPS and lowercase entries. Before the team can group revenue by city, the values need to be standardised.
Write a function clean_city(name) that returns the city name with leading and trailing whitespace removed and each word capitalised (first letter upper, rest lower).
This fixture runs before your code on every Run and Submit, so the data is always clean.
raw_cities = [" new delhi ", "BENGALURU", "navi mumbai", "hyderabad", " Pune"]- Step 1: `name.strip()` removes leading and trailing whitespace only — spaces *inside* the string are left alone, which is what we want.
- Step 2: `.title()` upper-cases the first letter of every word and lower-cases the rest, so both `BENGALURU` and `hyderabad` normalise correctly.
- Step 3: Chaining the two calls keeps the function a single expression. Strings are immutable in Python, so each call returns a new string rather than editing in place.
def clean_city(name):
return name.strip().title()Learn the concept first
Stuck on Strings & Text Cleaning? These free Python lessons teach it step by step.
More Strings & Text Cleaning questions
Asked at Zomato
Practising alone only goes so far — our live cohorts pair Python and Pandas depth with dashboards, agentic analytics projects and mock interviews.
Explore the program