Python for Excel users · Lesson 10 of 14

Excel text functions in pandas (.str)

LEFT, RIGHT, MID, LEN, TRIM, PROPER, CONCAT, SUBSTITUTE, FIND and Text to Columns in pandas, using the .str methods on a whole column.

About 16 minutes · code runs in your browser, nothing to install

Excel has a text function for every cleaning job: LEFT, TRIM, PROPER, SUBSTITUTE… pandas has the same set, reached through .str on a column. df["Name"].str.upper() works like =UPPER(A2) filled down.

LEFT, RIGHT, MID and LEN

Cutting text uses Python slicing [start:stop:step]: .str[:3] is =LEFT(A2, 3), .str[-4:] is =RIGHT(A2, 4). For =MID(A2, 5, 2) remember that Python counts from 0 and the end is not included, so it becomes .str[4:6].

Pythoneditable
import pandas as pd

codes = pd.Series(["LMP-2026-EU", "CHR-2025-US", "DSK-2026-UK"])
print(codes.str[:3].tolist())
print(codes.str[-2:].tolist())
print(codes.str[4:8].tolist())
print(codes.str.len().tolist())
Output
['LMP', 'CHR', 'DSK']
['EU', 'US', 'UK']
['2026', '2025', '2026']
[11, 11, 11]

=MID(A2, 5, 4) starts at character 5, which is position 4 in Python, and takes 4 characters: [4:8].

TRIM, UPPER, LOWER and PROPER

str.strip() removes spaces at both ends. Excel's TRIM also squeezes double spaces inside the text; in pandas split on spaces and join the words again with one space. .str.title() is PROPER.

Pythoneditable
import pandas as pd

names = pd.Series(["  ana  silva", "BEN   o'neill ", "chloe DUPONT"])
clean = names.str.split().str.join(" ")
print(clean.tolist())
print(clean.str.title().tolist())
print(clean.str.upper().tolist())
Output
['ana silva', "BEN o'neill", 'chloe DUPONT']
['Ana Silva', "Ben O'Neill", 'Chloe Dupont']
['ANA SILVA', "BEN O'NEILL", 'CHLOE DUPONT']

Like PROPER, title() capitalises after an apostrophe too: O'Neill.

CONCAT and & : joining text

=A2&" "&B2 becomes df["First"] + " " + df["Last"]. Numbers must be turned into text first with .astype(str), just as Excel turns them into text when you join them.

Pythoneditable
import pandas as pd

df = pd.DataFrame({
    "First": ["Ana", "Ben", "Chloe"],
    "Last": ["Silva", "Okafor", "Dupont"],
    "Id": [7, 12, 30],
})
df["Full"] = df["First"] + " " + df["Last"]
df["Login"] = df["First"].str.lower() + df["Id"].astype(str)
print(df)
Output
   First    Last  Id          Full    Login
0    Ana   Silva   7     Ana Silva     ana7
1    Ben  Okafor  12    Ben Okafor    ben12
2  Chloe  Dupont  30  Chloe Dupont  chloe30

Text to Columns and TEXTSPLIT

str.split() with expand=True splits each value into separate columns, like Data → Text to Columns or =TEXTSPLIT(A2, "-"). Give the new columns names and attach them to the table.

Pythoneditable
import pandas as pd

df = pd.DataFrame({"Code": ["LMP-2026-EU", "CHR-2025-US", "DSK-2026-UK"]})
df[["Type", "Year", "Market"]] = df["Code"].str.split("-", expand=True)
print(df)
Output
          Code Type  Year Market
0  LMP-2026-EU  LMP  2026     EU
1  CHR-2025-US  CHR  2025     US
2  DSK-2026-UK  DSK  2026     UK

=SUBSTITUTE(A2, "-", "/") is .str.replace("-", "/", regex=False). =FIND("-", A2) is .str.find("-"), which counts from 0 and gives -1 instead of #VALUE! when the text is missing. To test whether a cell contains a word, ignoring case like SEARCH, use .str.contains("word", case=False).

Pythoneditable
import pandas as pd

s = pd.Series(["Blue lamp", "Desk", "Small LAMP"])
print(s.str.replace(" ", "_", regex=False).tolist())
print(s.str.find("a").tolist())
print(s.str.contains("lamp", case=False).tolist())
Output
['Blue_lamp', 'Desk', 'Small_LAMP']
[6, -1, 2]
[True, False, True]

Two more: =TEXT(A2, "#,##0.00") is s.map("{:,.2f}".format) and =VALUE(A2) is pd.to_numeric(s). Keep numbers as numbers until the final report.

✎ Exercise

Clean the messy names (extra spaces, wrong capitals) into PROPER case, then build an Email column as first name, a dot and last name in lower case, plus @example.com. Print the table.

Hint: Clean with .str.split().str.join(" ").str.title(); split the lower-case name with expand=True and join parts[0] and parts[1].

Your codewrite your answer, then Run
import pandas as pd

df = pd.DataFrame({"Name": ["  ana   SILVA", "ben okafor  ", "CHLOE  dupont"]})
# your code here

Expected output:

           Name                     Email
0     Ana Silva     ana.silva@example.com
1    Ben Okafor    ben.okafor@example.com
2  Chloe Dupont  chloe.dupont@example.com
Show a solution
Solutioneditable
import pandas as pd

df = pd.DataFrame({"Name": ["  ana   SILVA", "ben okafor  ", "CHLOE  dupont"]})
df["Name"] = df["Name"].str.split().str.join(" ").str.title()
parts = df["Name"].str.lower().str.split(" ", expand=True)
df["Email"] = parts[0] + "." + parts[1] + "@example.com"
print(df)
Output
           Name                     Email
0     Ana Silva     ana.silva@example.com
1    Ben Okafor    ben.okafor@example.com
2  Chloe Dupont  chloe.dupont@example.com

Check yourself

1. Which pandas code is =LEFT(A2, 2)?

2. What is =MID(A2, 3, 2) in pandas?

3. What does .str.find("x") return when the text has no x?

Spotted a mistake on this page? Tell us