String Operations with .str

The .str accessor in pandas is a gateway that lets you apply familiar Python string methods, such as lower, strip, contains, and split, to every value in a text column at once, returning a new Series of results.

Learn String Operations with .str in our free Pandas course — a beginner-friendly interactive lesson with worked examples, a practice exercise and a quick…

Part of the free Pandas course at LearnCodingFast — hands-on lessons with examples you run in your browser, plus practice exercises and a quick quiz.

Learn to clean messy text, search and filter rows by their contents, split fields into pieces, and chain operations into tidy pipelines.

Real-world text is messy: mixed capitalisation and stray spaces are everywhere. The .str accessor exposes the usual Python methods. .str.lower() and .str.upper() change case, .str.strip() removes leading and trailing spaces, and .str.len() counts characters per value.

.str.contains("x") returns a boolean Series you can use as a filter mask. .str.startswith() checks the beginning of each value, and .str.replace(old, new) swaps text everywhere it appears. These three power most text filtering and cleaning tasks.

.str.split(sep, expand=True) breaks one column into several. With expand=True you get a DataFrame with one column per piece, ready to assign to new names. Because each .str call returns a Series, you can chain several together in one readable line.

Replace the blank so every tag is lowercase with no surrounding spaces. The expected output is the list ['python', 'pandas', 'data'].

Clean a name column and turn it into lowercase usernames, then filter to names containing the letter "a".

Lesson complete — text columns hold no fear!

You can clean casing and whitespace, search and filter with contains, replace text, split fields into columns, and chain it all together. Remember that missing values turn into NaN, so reach for na=False when filtering.

🚀 Up next: Working with Dates & Times — parse and slice temporal data.

Practice quiz

What does the .str accessor let you do?

  • Sort a column
  • Convert a column to numbers
  • Apply string methods to every value in a text column
  • Rename columns

Answer: Apply string methods to every value in a text column. The .str accessor applies Python string methods across the whole Series.

Which call lowercases every value in a text column?

  • s.lower()
  • s.str.toLowerCase()
  • s.casefold()
  • s.str.lower()

Answer: s.str.lower(). s.str.lower() applies lower() to each element of the Series.

What does .str.strip() remove?

  • Leading and trailing whitespace
  • All spaces inside the text
  • Every vowel
  • Numeric characters

Answer: Leading and trailing whitespace. .str.strip() trims whitespace from the start and end of each value.

What does .str.contains('gmail') return?

  • The matching substring
  • A boolean Series usable as a filter mask
  • The count of matches
  • A new column

Answer: A boolean Series usable as a filter mask. It returns a boolean Series, True where the substring appears.

How do you split one column into several columns?

  • s.str.split(' ')
  • s.str.partition()
  • s.str.split(' ', expand=True)
  • s.str.divide(' ')

Answer: s.str.split(' ', expand=True). expand=True returns a DataFrame with one column per split piece.

Why might .str.contains return NaN for some rows?

  • The pattern was too long
  • The column was numeric
  • contains is deprecated
  • Those rows held missing (NaN) values

Answer: Those rows held missing (NaN) values. String methods cannot operate on NaN, so they return NaN there.

Which argument treats missing rows as False in .str.contains?

  • na=False
  • skipna=True
  • fillna=False
  • dropna=True

Answer: na=False. na=False makes missing rows count as False for clean filtering.

Which method swaps text everywhere it appears in each value?

  • s.str.swap(a, b)
  • s.str.replace(a, b)
  • s.str.sub(a, b)
  • s.str.change(a, b)

Answer: s.str.replace(a, b). .str.replace(old, new) substitutes text in every value.

What does .str.len() return for a text column?

  • The number of rows
  • The longest value
  • The unique values
  • The character count of each value

Answer: The character count of each value. .str.len() gives the length of each string in the Series.

Why can you chain .str.strip().str.lower() in one line?

  • Each .str call returns a Series you can keep operating on
  • strip resets the index
  • pandas caches the result
  • lower must always follow strip

Answer: Each .str call returns a Series you can keep operating on. Because every .str method returns a Series, calls chain naturally.