re.sub()

Published Jul 30, 2021Updated Sep 5, 2023
Contribute to Docs

The re.sub() function replaces 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 the 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”:

us
Visit us
code
Hide code
Code
Output
Hide output
Hide output
Loading...

All contributors

Looking to contribute?

Learn Python on Codecademy