▶ Sign up free for the video, slides & prompts
The lesson below is open to all. Enter your email once to unlock every video, slide deck, prompt and code example on this device.
Checking…
String methods for cleaning text
split, strip, lower and replace on messy input.
🎬 Video version coming soon — read it now
Data typed by real people is messy: extra spaces, odd capitals, commas everywhere. Cleaning it is a big part of every data and AI job, and string methods do most of the work.
The cleaning toolkit
split and join
split() breaks text into a list; join() glues a list back into text:
With no argument, split() splits on any run of spaces, which is great for words.
Checking what text contains
A real cleaning job
Phone numbers typed into a sign-up form:
Strings never change in place
Every method returns a new string. Remember to store it: name = name.strip().title(). Chaining methods like this is normal and readable.
" Vizag".lower() == "vizag" is False because of the space; .strip().lower() fixes it.In one line
strip, lower, title, replace, split and join clean almost any messy text, and each one returns a new string.
Share with a friend