Finding and Replacing Text in Python
Finding and replacing text is one of the most common and powerful operations in data science. Whether you are cleaning messy data, standardizing names, correcting typos, extracting patterns, or preparing text for machine learning, knowing how to find and replace efficiently is essential. In 2026, Python offers both simple string methods and powerful Regular Expressions to handle these tasks cleanly and scalably.
TL;DR — Key Techniques
.find(),.index(),in→ locate text.replace(old, new)→ simple string replacementre.search()andre.sub()→ powerful regex-based find and replace- pandas
.str.contains()and.str.replace()→ vectorized operations
1. Basic Finding and Replacing with String Methods
text = "Data Science is fun and powerful for data analysis"
print(text.find("data")) # first occurrence
print("science" in text.lower()) # case-insensitive check
clean = text.replace("fun", "powerful")
print(clean)
2. Real-World Data Science Examples
import pandas as pd
# Demo file so this example runs as-is (in your project this is your real export)
pd.DataFrame({
"customer_name": [" alice smith ", "BOB JONES", " carol diaz Inc."],
"first_name": ["Alice", "Bob", "Carol"],
"last_name": ["Smith", "Jones", "Diaz"],
"email": [" Alice@Example.com", "bob@company.org", "carol@data.net "],
"region": ["N.Y.", "California", "N.Y."],
"company": ["Acme Inc.", "Globex Inc.", "Initech"],
"product_code": ["EL-101 ", "BK-202", "#HM-303"],
"product_category": ["electronics", "books", "home"],
"date_str": ["2026-03-19", "2025-11-02", "2026-01-15"],
"description": ["Premium laptop for machine learning and data science",
"Data book: data cleaning with Python",
"Contact sales@shop.com for premium support"],
"log": ["2026-03-19 - INFO - login", "2026-03-19 - ERROR - timeout", "2026-03-20 - INFO - logout"],
}).to_csv("customer_data.csv", index=False)
df = pd.read_csv("customer_data.csv")
# Example 1: Simple replacement
df["customer_name"] = df["customer_name"].str.replace("Inc.", "").str.strip()
# Example 2: Find and flag patterns
df["has_email"] = df["description"].str.contains("@", na=False)
# Example 3: Advanced replacement with regex
df["product_code"] = df["product_code"].str.replace(r"[^A-Z0-9]", "", regex=True)
3. Regex-Powered Finding and Replacing
import re
text = "Order ID: ORD-12345, Amount: $1250.75"
# Find with regex
match = re.search(r"ORD-(\d+)", text)
if match:
print("Order ID found:", match.group(1))
# Replace with regex
clean = re.sub(r"\$\d+\.\d{2}", "[REDACTED]", text)
print(clean)
4. Best Practices in 2026
- Use simple string methods for basic find/replace tasks
- Switch to regex (
re.search/re.sub) for complex patterns - Use pandas
.strmethods for vectorized operations on DataFrames - Always normalize case before searching/replacing when needed
- Keep original columns and create cleaned versions for traceability
Literal Replacement Versus Pattern Replacement
str.replace replaces exact text, while re.sub replaces anything that matches a pattern. In pandas, Series.str.replace treats the pattern as literal text by default since pandas 2.0, so you must pass regex=True to use regular expressions.
Literal replacement is faster and safer when the text is fixed. Pattern replacement is needed when the target varies, such as different numbers of spaces or several spellings.
Both accept a count limit: replace(old, new, 1) and re.sub(pattern, repl, text, count=1) change only the first occurrence.
Worked Example: Cleaning Phone Numbers
Phone numbers are cleaned with literal and pattern replacements.
import re
import pandas as pd
phones = pd.Series(["(021) 555-0101", "021.555.0102", "021 555 0103"])
digits = phones.str.replace(r"\D", "", regex=True)
print(digits.tolist())
print(phones.str.replace(".", "-", regex=False).tolist())
print(re.sub(r"\s+", " ", "a b c", count=1))
The regex \D removes every non-digit, giving clean ten-digit strings. The literal replacement changes only dots, and count=1 limits re.sub to the first run of spaces.
Practical Tips
Collect replacement rules in a list of pairs and apply them in a loop. This keeps the order explicit, makes it easy to add a rule, and lets you print each rule's effect when debugging.
Compare value_counts before and after the cleaning step to see which values changed.
Common Mistakes
- Forgetting regex=True when using patterns in pandas.
- Using a regex when literal text would do.
- Forgetting that a dot in a regex matches any character.
- Replacing in the wrong order so later rules undo earlier ones.
Quick Summary
- replace changes exact text.
- re.sub changes pattern matches.
- pandas needs regex=True for patterns.
- count limits how many changes happen.
Conclusion
Finding and replacing text is a foundational skill that bridges basic string operations and full Regular Expressions. In 2026 data science projects, combine Python’s built-in methods, pandas .str accessor, and re.sub() to clean, standardize, and transform text data efficiently. These techniques make your preprocessing pipelines cleaner, faster, and more professional.
Next steps:
- Review your current text-cleaning code and apply find/replace operations using both simple methods and regex where appropriate