Skip to main content
Albero Academy

String Methods

Clean and extract text with the vectorised .str accessor.

Cleaning & Transforming8 min

Text columns need cleaning — trimming whitespace, fixing case, splitting, extracting. The .str accessor applies Python string methods to a whole column at once, vectorised.

Cleaning text

.str methods
df["name"].str.strip() # trim whitespace
df["name"].str.lower() # lowercase
df["name"].str.title() # Title Case
df["email"].str.contains("@") # boolean mask
df["code"].str.startswith("PY")

Splitting & extracting

split, extract, replace
df["email"].str.split("@").str[1] # the domain
df["phone"].str.replace("-", "") # strip separators
df["id"].str.extract(r"(\d+)") # pull digits with a regex

Key takeaways

  1. 01The .str accessor applies string methods to a whole column, vectorised.
  2. 02Clean with strip/lower/title; test with contains/startswith.
  3. 03split, extract (regex) and replace parse and transform text.
  4. 04Chain .str calls to build readable cleaning pipelines.

Finished this chapter?

Mark it complete to track your progress through Pandas.