re.sub()

BrandonDusch's avatar
Published Jul 30, 2021Updated Oct 14, 2024
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>)
  • <pattern>: A regular expression pattern used to match substrings.
    • A string: Jane Smith
    • Character class codes: /w, /s, /d
    • Regex symbols: $, |, ^
  • <replacement>: The replacement argument. This can either be a string or a function.
  • <count>: An integer specifying the number of occurrences to replace. The default is to replace all matches.
  • <flags>: Specifies additional options such as IGNORECASE, VERBOSE, DOTALL, etc.

Example

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

import re
def replace_business_intelligence(match):
return 'business intelligence'
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.'''
# Use the function `replace_bussines_intelligence` as the replacement argument in re.sub()
match = re.sub(r'bi', replace_business_intelligence, blurb, flags=re.IGNORECASE)
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”:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn Python on Codecademy