Skip to content
Justice Tech School

String Methods

The .str accessor applies string methods to every value: lower, upper, strip, contains, replace, split and len.

Example
import pandas as pd
import numpy as np
s = pd.Series([' Ada Lovelace ', 'bob smith', None])
t = s.str.strip().str.title()
print(t.tolist())
print(s.str.contains('smith').tolist())
print(s.str.len().tolist())
print(t.str.split(' ').str[0].tolist())

Result

['Ada Lovelace', 'Bob Smith', nan]
[False, True, False]
[14.0, 9.0, nan]
['Ada', 'Bob', nan]

Exercise

Make lowercase.

s.str.()

Quiz Test

What does .str.contains return?

These free lessons are for learning. Join a full course for projects, mentor feedback and a verified certificate.