Searching for text, counting the matches, and replacing what you found are the three jobs that every real program handling text eventually needs to do. Python gives you two layers for that work. Plain string methods handle fixed substrings, and the re module handles patterns. The confusing part is not the syntax, but the difference in failure behaviour, because find() and index() do the same job and they disagree about what should happen when the text is not there.
The previous lesson in this module covered "Why Strings Are Immutable (and Why It Matters)", and the next lesson in this module is about "Splitting, Joining and Parsing Text Data".
How do I search for a substring?
Three tools, and the choice between them is about how you want failure to look. The in operator answers a yes-or-no question. find() tells you where the text starts, or gives you -1 when it is missing. index() tells you the same position, but it raises ValueError instead of returning -1.
text = 'the quick brown fox jumps over the lazy dog'
print('contains ->', 'fox' in text)
print('find ->', text.find('fox'))
print('find miss ->', text.find('cat'))
print('rfind ->', text.rfind('the'))
try:
text.index('cat')
except ValueError as error:
print('index miss ->', error)
print('count ->', text.count('the'))
print('startswith ->', text.startswith('the'))
print('endswith ->', text.endswith('dog'))
The choice between -1 and an exception is a decision that you have to make on purpose. -1 is convenient, but it is easy to forget to check it, because text.find('cat') + 1 quietly gives you 0 instead of failing. An exception is loud, and it is the better choice when a missing substring means that the program is wrong and not merely unlucky.
One more detail about count() is worth knowing. It counts occurrences that do not overlap each other, so the string 'aaa' contains two occurrences of 'aa' and not three.
The most common use of find() is not the position itself but the slice around it, and that is how you pull a value out of a line of text.
line = 'user=ada; action=login'
start = line.find('user=') + len('user=')
end = line.find(';', start)
print('extracted ->', line[start:end])
Searching from the right works in the same way with rfind() and rindex(), and that is what you want for a path or a URL where the last occurrence is the interesting one. When a string may contain several different separators, re.split() handles them all at once, which the plain split() cannot do.
How do I replace text?
str.replace() replaces every occurrence of a literal substring. There is no count argument, so if you want to change only the first occurrence you need a different approach.
import re
text = '2026-10-27'
print('replace ->', text.replace('-', '/'))
print('delete ->', text.replace('-', ''))
print('no match ->', text.replace('x', 'y'))
print('sub once ->', re.sub(r'\d', '*', 'a1b2c3', count=1))
print('sub all ->', re.sub(r'\d', '*', 'a1b2c3'))
Replacing with an empty string deletes the text, and that is the quickest way to strip characters out of a string. When you need a limit, a pattern, or a replacement that you compute, re.sub() is the tool. It takes a pattern instead of a literal, and its count argument caps how many replacements it makes.
The replacement can also be a function, and that is how you change each match as you find it.
import re
text = 'order 12 and order 7'
print('found ->', re.findall(r'\d+', text))
def double(match):
"""Return the matched number twice as large."""
return str(int(match.group()) * 2)
print('doubled ->', re.sub(r'\d+', double, text))
When do you need regular expressions?
Reach for the re module when the thing you are looking for has a structure rather than being a fixed string. Three functions cover most needs, and the difference between them is where they start matching.
re.match(pattern, text)matches only at the very beginning of the string.re.search(pattern, text)looks anywhere in the string and returns the first match.re.fullmatch(pattern, text)requires the whole string to match.
import re
pattern = r'\w+@\w+\.\w+'
text = 'write to [email protected] or [email protected] today'
found = re.search(pattern, text)
print('search ->', found.group() if found else 'no match')
print('match ->', re.match(r'\w+', text).group())
print('fullmatch ->', re.fullmatch(r'\w+', text))
print('findall ->', re.findall(pattern, text))
for found in re.finditer(pattern, text):
print('finditer ->', found.group(), 'at', found.span())
findall() returns a list of the matched text, while finditer() returns match objects that know where each match was, and that is what you want when you need the surrounding context. A match object also carries group() for the whole match and groups() for any capture groups inside the pattern, together with start() and end() for the character positions.
import re
text = 'Ada Lovelace, Grace Hopper, Alan Turing'
pattern = r'(?P\w+) (?P\w+)'
for match in re.finditer(pattern, text):
print(match.group('first'), match.group('last'), '| all groups:', match.groups())
Named groups, written as (?P<name>...), are worth the extra characters, because they let you refer to a part of the match by name instead of by position. The names also show up in error messages when the pattern does not match what you expected.
There are two warnings about regular expressions. The first one is that they are slower than plain string methods, because the pattern has to be compiled and then matched, so a fixed substring that appears many times should be handled with str.find() or str.replace(). The second one is that a pattern is much harder to review than a plain comparison, and patterns grow. A pattern with nested groups and lookaheads is usually better written as a little ordinary Python code, which is easier to read and much easier to test.
How do you search case-insensitively?
There are two answers, and they suit different situations. To compare two strings, fold both of them with casefold() and use ==. To search inside a larger text, pass the re.IGNORECASE flag. That same flag is how you switch on multi-line matching, on dot-matches-newline behaviour, and on verbose patterns.
import re
text = 'Python is great. PYTHON is everywhere.'
print('ignorecase ->', len(re.findall('python', text, re.IGNORECASE)))
print('casefold ->', text.casefold().count('python'))
print('multiline ->', re.findall(r'^\w+', 'one\ntwo\nthree', re.MULTILINE))
needle = 'c++ (lang)'
haystack = 'we use c++ (lang) here'
print('escaped ->', re.findall(re.escape(needle), haystack))
The last line matters more than it looks. A regular expression treats characters like + and ( as syntax, so searching for a literal string that contains them will either fail or match something that you did not want. re.escape() quotes the string so that it is matched literally, and it is the right habit whenever the text you are searching for comes from a user.
Your turn: build a log scrubber
- Write a function that takes a log line and removes anything that looks like an email address, using
re.sub()with a replacement pattern. Check that the rest of the line survives. - Extend it to replace the digits in the line with asterisks, but only the first three, using the
countargument. Print both versions next to each other. - Now find every timestamp in a line, using
re.findall()with a pattern for digits and colons, and print them as a list. - Add a case-insensitive search for a given word and compare the result with
text.casefold().count(word). Explain in a comment when the two answers differ. - Finally, write a version that redacts a word that the user supplies, making sure that you call
re.escape()on it. Test it with the wordc++and show that the pattern still matches literally.
Search and replace reference
inanswers a yes-or-no question,find()returns a position or-1, andindex()raisesValueError.count()counts occurrences that do not overlap each other.str.replace()replaces every occurrence, and you usere.sub()when you need a pattern, a limit, or a replacement that you compute.re.matchanchors at the start,re.searchlooks anywhere, andre.fullmatchrequires the whole string.- Use named groups for readability, and use
re.escape()whenever the pattern comes from user input. - Compare with
casefold()and search with there.IGNORECASEflag.