String data is common in datasets, and it often needs to be cleaned, transformed, searched, or split before analysis. Pandas provides the str accessor, which allows string operations to be applied to values across a Series or Index.
These methods are useful for tasks such as standardizing text, removing unwanted spaces, replacing values, filtering rows, and extracting information from strings.
Creating Sample Series
Example Below use a small series containing names and text values.
import pandas as pd
data = pd.Series(
[" Virat ", "Azam", "Finch", "Shakib", "Stokes", "Kane"],
dtype="string"
)
print(data)
Output
0 Virat 1 Azam 2 Finch 3 Shakib 4 Stokes 5 Kane dtype: string
Let's explore each method in detail with example:
1. Lower
This method converts all alphabetic characters in each value to lowercase. It is commonly used when text needs a consistent format before comparison, filtering, or grouping.
print(data.str.lower())
Output:

2. Upper
This method converts alphabetic characters to uppercase. It can be useful when standardizing codes, categories, labels, or other text fields.
print(data.str.upper())
Output:

3. strip()
It removes leading and trailing whitespace or other specified characters from each string. It is commonly used when imported data contains accidental spaces around values.
import pandas as pd
data = pd.Series(
[" Alice ", " Bob", "Charlie "],
dtype="string"
)
print(data.str.strip())
Output
0 Alice 1 Bob 2 Charlie dtype: string
4. replace()
It replaces matching text with another value. In current Pandas, regex=False is the default, so simple string patterns are treated literally.
import pandas as pd
data = pd.Series(
["Python Developer", "Python Analyst", "Java Developer"],
dtype="string"
)
print(data.str.replace("Python", "Data", regex=False))
Output
0 Data Developer 1 Data Analyst 2 Java Developer dtype: string
5. split()
This method divides each string around a specified separator. The result is normally a Series containing lists, and the expand parameter can be used when the parts should become separate columns.
import pandas as pd
data = pd.Series(
["Alice-Data", "Bob-ML", "Charlie-Python"],
dtype="string"
)
print(data.str.split("-"))
Output
0 [Alice, Data] 1 [Bob, ML] 2 [Charlie, Python] dtype: object
6. contains()
Checks whether each string contains a given substring or pattern. It returns Boolean values and is commonly used for filtering text-based data.
import pandas as pd
data = pd.Series(
["Python", "Pandas", "NumPy", "Java"],
dtype="string"
)
print(data.str.contains("Py"))
Output
0 True 1 False 2 True 3 False dtype: boolean
7. startswith()
It checks whether each value begins with the specified pattern. It returns Boolean results for the individual elements.
import pandas as pd
data = pd.Series(
["Python", "Pandas", "NumPy", "Java"],
dtype="string"
)
print(data.str.startswith("Py"))
Output
0 True 1 False 2 False 3 False dtype: boolean
8. endswith()
Checks whether each string ends with a specified pattern. It is useful when filtering values according to suffixes such as file extensions or codes.
import pandas as pd
files = pd.Series(
["report.csv", "image.png", "data.csv", "notes.txt"],
dtype="string"
)
print(files.str.endswith(".csv"))
Output
0 True 1 False 2 True 3 False dtype: boolean
9. len()
This method returns the number of characters in each string. It is useful when validating text lengths or identifying unusually short or long values.
import pandas as pd
data = pd.Series(
["Alice", "Bob", "Charlie", "David Smith"],
dtype="string"
)
print(data.str.len())
Output
0 5 1 3 2 7 3 11 dtype: Int64
10. find()
This method returns the position of the first occurrence of a specified substring. If the substring is not found, it returns -1.
import pandas as pd
data = pd.Series(
["Python", "Pandas", "NumPy", "Java"],
dtype="string"
)
print(data.str.find("a"))
Output
0 -1 1 1 2 -1 3 1 dtype: Int64