re.sub()

Replace matching substrings with a new string for all occurrences, or a specified number.

Syntax

re.sub(<pattern>, <replacement>, string, <count>, <flags>)

A <pattern> is a regular expression that can include any of the following:

  • A string: Jane Smith
  • A character class code: /w, /s , /d
  • A regex symbol: $, |, ^

The other arguments include:

  • The replacement string (<replacement>): foo
  • An integer value for the number of replacements (<count>): 10
  • <flags>: IGNORECASE, VERBOSE, DOTALL

Example

The following example replaces all occurrences of “BI” with “business intelligence”:

import re
blurb = '''The analytics firm uses a range of BI tools to visualize data. Their internal data science team suggests
bi tools may be their most valuable resource.'''
match = re.sub(r'bi','business intelligence',blurb,flags=re.IGNORECASE)
# The ignorecase flag allows for matches regardless of case
print(match)

This will print the following where “bi” is replaced with “business intelligence”:

The analytics firm uses a range of business intelligence tools to visualize data. Their internal data science team suggests
business intelligence tools may be their most valuable resource.

Codebyte Example

Replace all numerical values with “REDACTED”:

Interested in helping build Docs? Read the Contribution Guide or share your feedback form.

Learn Python on Codecademy