Skip to content
IT Vizag
← Lists, Dicts & Data

▶ 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

Lesson 7 of 9 · 1 min read

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

🔒 Code example: sign up free (above) to unlock it.

split and join

split() breaks text into a list; join() glues a list back into text:

🔒 Code example: sign up free (above) to unlock it.

With no argument, split() splits on any run of spaces, which is great for words.

Checking what text contains

🔒 Code example: sign up free (above) to unlock it.

A real cleaning job

Phone numbers typed into a sign-up form:

🔒 Code example: sign up free (above) to unlock it.

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.

Tip: Always clean text before comparing it. " 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.

Saved on this device. Sign in to keep progress everywhere.

Share with a friend