Index Function in Python – Finding Substring Positions for Data Science 2026
The str.index() method is a fundamental string operation that returns the lowest index where a substring is found. In data science, it is frequently used for locating specific patterns, extracting text by position, validating data formats, and preparing strings before applying Regular Expressions. Understanding .index() alongside its safer counterpart .find() is essential before moving into more advanced regex-based searching.
TL;DR — Index vs Find
string.index(sub)→ returns starting index (raises ValueError if not found)string.find(sub)→ returns starting index or -1 if not found (safer)- Both support optional
startandendparameters - Use
.index()when you expect the substring to exist
1. Basic Usage of .index()
text = "Python is great for data science and machine learning"
print(text.index("data")) # 20
print(text.index("science")) # 25
# With start position
print(text.index("data", 10)) # search from index 10
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: Find position of "@" in emails
df["at_position"] = df["email"].str.index("@")
# Example 2: Extract domain using index
df["domain"] = df["email"].apply(
lambda x: x[x.index("@")+1:] if "@" in x else None
)
# Example 3: Safe usage with try/except or .find()
def safe_index(s, sub):
try:
return s.index(sub)
except ValueError:
return -1
3. Index vs Find – When to Choose Which
text = "Python is great for data science"
# .index() - use when you are sure the substring exists
idx = text.index("data")
# .find() - safer when the substring may be missing
pos = text.find("missing")
print(pos) # -1
4. Best Practices in 2026
- Use
.find()for most cases to avoid exceptions - Use
.index()only when the substring is guaranteed to exist - Combine with pandas
.str.index()for vectorized operations - Always handle the possibility of missing substrings gracefully
- Use indexing as a stepping stone before moving to Regular Expressions
index() Versus find() in Real Code
index() and find() return the same position when the substring exists. The difference is what happens when it does not: find returns -1, while index raises ValueError. Use index when a missing value is a real error, and find when it is expected.
Both accept optional start and end positions, which lets you search for the next occurrence after a previous one. rindex and rfind search from the right.
A common bug is slicing with the result of find without checking for -1, which quietly returns the wrong part of the string.
Worked Example: Extracting Text Between Markers
We take the text between two brackets and handle missing markers.
def between(text, left="[", right="]"):
try:
start = text.index(left) + 1
end = text.index(right, start)
except ValueError:
return None
return text[start:end]
print(between("order [A-17] shipped"))
print(between("no markers here"))
print("a-b-c".rindex("-"), "abc".find("z"))
index raises when a marker is missing, and the function turns that into None instead of returning a wrong slice. rindex finds the last dash at position 3, and find returns -1 for a missing letter.
Common Mistakes
- Slicing with find results without checking for -1.
- Letting index errors crash a whole pipeline.
- Forgetting the start argument when searching for the second marker.
- Using string positions on data with varying formats.
Quick Summary
- find returns -1 when missing.
- index raises ValueError when missing.
- start and end limit the search.
- rfind and rindex search from the right.
Conclusion
The index() function is a fast and precise way to locate substrings in Python. In 2026 data science workflows, it is commonly used alongside .find() for position-based text extraction and data cleaning. Mastering these methods builds a strong foundation for more advanced Regular Expression techniques and makes your text processing pipelines cleaner and more efficient.
Next steps:
- Review your current text columns and add index-based features for pattern location and extraction