Finding and Replacing Text in Python

Regular Expressions Mar 19, 2026

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 replacement
  • re.search() and re.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 .str methods 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

Related Tutorials

Share:
Last updated: March 2026
Built-in Functions in Python 2026 str() in Python 2026: String Conversion + Modern Formatting & Best Practices memoryview with JAX in Python 2026: Zero-Copy NumPy → JAX Array Interop + Efficient ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + GPU Pinning & ML Examples memoryview with TensorFlow in Python 2026: Zero-Copy NumPy → Tensor Interop + ML Examples memoryview with NumPy & PyTorch in Python 2026: Zero-Copy Views, Efficient Slicing & ML Interop Examples memoryview with NumPy in Python 2026: Zero-Copy Views, Efficient Slicing & Real ML Examples memoryview() in Python 2026: Zero-Copy Magic for Large Binary Data + Real Examples import() in Python 2026: How It Works, Security Risks & Modern Alternatives (importlib) zip() in Python 2026: How to Use It, strict=True Behavior & Real-World Examples vars() in Python 2026: Accessing Object Namespace + Modern Introspection Patterns type() in Python 2026: Dynamic Type Inspection & Object Creation + Modern Patterns tuple() in Python 2026: Immutable Sequences + Modern Patterns & Best Practices super() in Python 2026: Method Resolution & Modern Inheritance Patterns sum() in Python 2026: Summing Iterables + Modern Numeric Patterns & Best Practices staticmethod() in Python 2026: Static Methods, Utility Functions & Modern Best Practices sorted() in Python 2026: Sorting Iterables + Modern Patterns & Best Practices slice() in Python 2026: Creating Slice Objects + Modern Patterns & Best Practices setattr() in Python 2026: Dynamic Attribute Setting + Modern Patterns & Safety set() in Python 2026: Mutable Sets Creation + Modern Patterns & Best Practices