Strings — Manipulation & Methods
Data analysts deal with messy text every day — customer names, product codes, city names with inconsistent casing. String methods are not optional.
How do you convert a string to uppercase and lowercase?
s = "Hello World"
s.upper() # "HELLO WORLD"
s.lower() # "hello world"
s.title() # "Hello World"In data cleaning, .lower() is used to standardise column values before grouping — e.g. "Mumbai" and "MUMBAI" should count as the same city.
How do you remove whitespace from a string?
s = " hello "
s.strip() # "hello" — both sides
s.lstrip() # "hello " — left only
s.rstrip() # " hello" — right onlyLeading/trailing spaces are one of the most common data quality issues in CSV files. Always .strip() string columns after import.
How do you split a string into a list?
s = "Delhi,Mumbai,Noida"
parts = s.split(",") # ["Delhi", "Mumbai", "Noida"]
# Rejoin:
",".join(parts) # "Delhi,Mumbai,Noida"split() is used to parse delimited fields — comma-separated tags, pipe-separated codes. join() is the reverse and is faster than string concatenation in loops.
How do you check if a string contains a substring?
s = "data analyst noida"
"noida" in s # True
s.find("noida") # 14 (index) or -1 if not found
s.startswith("data") # True
s.endswith("ida") # TrueThe in operator is the Pythonic way. In Pandas, use df["city"].str.contains("Noida", case=False) to filter rows with partial text matches.
How do you replace part of a string?
s = "data_analyst_noida"
s.replace("_", " ") # "data analyst noida"
# Pandas column:
df["name"] = df["name"].str.replace("Ltd.", "Limited").replace() on a string replaces all occurrences. In Pandas, .str.replace() supports regex — powerful for cleaning phone numbers, product codes, etc.
How do you count occurrences of a character in a string?
s = "banana"
s.count("a") # 3
len(s) # 6.count() is useful in text analysis. len() gives total length — you can use it to filter out entries that are too short (likely invalid) or too long (possibly dirty data).
EVIKA ACADEMY · PYTHON FOR DATA ANALYTICS
Want to master Python with live practice?
Join our Python for Data Analysis course — live classes in Noida and online across India.
Book Free Demo Class →