Manipulate text data — the most common task in data cleaning
Strings (text data) are everywhere in data analysis — product names, city names, customer notes, email addresses, categories. Python has powerful built-in string methods that make text cleaning fast.
In data analyst work, you will constantly: strip extra spaces, change case, split strings by a delimiter, replace values, and check if a string contains a keyword. All of these are one-liners in Python.
Examples
Most-used string methods for data cleaning
text = " Data Analyst - Noida "
# Strip whitespace (very common in CSV data)
text.strip() # "Data Analyst - Noida"
text.lstrip() # strip left only
text.rstrip() # strip right only
# Case conversion
text.strip().upper() # "DATA ANALYST - NOIDA"
text.strip().lower() # "data analyst - noida"
text.strip().title() # "Data Analyst - Noida"
# Replace
text.replace("-", ":") # " Data Analyst : Noida "
# Split into a list
"Delhi,Noida,Gurgaon".split(",")
# ['Delhi', 'Noida', 'Gurgaon']
# Check contains
"Noida" in text # True
text.startswith(" D") # True
text.endswith(" ") # True
# Find length
len("Noida") # 5
f-strings — the best way to format output
name = "Priya"
salary = 75000
city = "Noida"
# Old way (avoid)
print("Name: " + name + ", Salary: " + str(salary))
# f-string (use this always)
print(f"Name: {name}, Salary: ₹{salary:,}, City: {city}")
# Output: Name: Priya, Salary: ₹75,000, City: Noida
# Format numbers
revenue = 4523678.5
print(f"Revenue: ₹{revenue:,.2f}")
# Output: Revenue: ₹4,523,678.50
💡 f-strings (formatted string literals) are the standard in modern Python. Use them over + concatenation.
Key Points
✓str.strip() removes leading/trailing spaces — always apply when reading CSV data
✓str.lower() / str.upper() for case-normalisation before merging or filtering
✓str.split(",") converts "a,b,c" into ["a","b","c"] — common for multi-value columns
✓f-strings: f"Hello {name}" — cleaner and faster than string concatenation
✓In pandas: df["City"].str.strip().str.lower() — chain string methods on columns
Practice Question
A city column has values like " delhi ", " NOIDA ", "Gurgaon". Which pandas operation standardises all values to lowercase without spaces?