Replacing String with Substring in Python

To replace a string with a substring in Python, you can use the built-in string.replace(old, new [, count])) method. This method returns a copy of the string in which the old substring has been replaced by the new substring and does not change the original (strings in Python are immutable). If the substring is not found, a copy of the original string is returned. Optionally, you can limit the number of replacements by specifying the "count" argument. In this case, only the first "count" occurrences will be replaced. If the "count" argument is not specified, then all occurrences of the substring will be replaced. In this Python String Replace Example, we use the string.replace() method to change all occurrences of a substring in the string. Click Execute to run the Python String Replace Example online and see the result
Replacing String with Substring in Python Execute
str = "I like programming in JavaScript"

a = str.replace("JavaScript", "Python")

print(a)
Updated: Viewed: 3124 times

What is a string in Python?

In Python, a string consists of an ordered sequence of characters. By default, Python 3's string type is a 16-bit byte array encoded in UTF-8. String characters can be accessed by index using square brackets "[]". Strings in Python are immutable; that is, once created, the string cannot be changed. Each string manipulation method returns a copy of the string and does not alter the original. The built-in "str" library has an extensive set of methods for working with strings. This library provides methods for searching, concatenating, reversing, splitting, and comparing strings.

Python String Replace Syntax

Following is the syntax of replace() method:

Python replace() Syntax
str.replace(old, new, count)

Where:
  • old: the part of the string to replace
  • new: the new substring that will replace the old substring.
  • count (optional): a number indicating how many times you want to replace the old substring with the new substring. By default all occurrences will be replaced.

Python String Replace Examples

Below are examples of replacing strings in Python:

Replace all occurrences of a string in Python

The following is an example of replacing all occurrences of a string in a Python string:

Python Replace String Example
str = "just do it, just do it, just do it"

a = str.replace("just", "simply")

print(a)

# output: simply do it, simply do it, simply do it

Replace a specific number of occurrences of a string in Python

The following is an example of replacing a specific number of occurrences of a substring in a Python string:

Python String Replace Example: Number of Occurrences
str = "just do it, just do it, just do it"

a = str.replace("just", "simply", 2)

print(a)
      
# output: simply do it, simply do it, just do it

Replace the first character in Python string

The following is an example of replacing the first occurrence of a substring in a Python string:

Python Replace First Character in String Example
str = "Python"

a = str.replace('P', 'I', 1)

print(a)
      
# output: Iython

Replace all characters in Python string

The following is an example of replacing all characters in a Python string:

Python Replace All Character in String Example
str = "Apple"

a = str.replace("Apple", "Cherry")

print(a)
      
# output: Cherry

How to replace a Python string with a Regular Expression

Python has a built-in regular expression module that can be used to search and replace strings. To use the regex module, you must import it at the beginning of your code.

import re

The regex module has a built-in re.sub() method for searching and replacing strings. This function takes a regex pattern, string to replace and a new value and replaces the old value with the new value using the provided pattern.

Python re.sub() Syntax
re.sub(pattern, replacement, string, count=0, flags=0)

Where:
  • pattern: the pattern for searching within a string with a regular expression
  • replacement: the variable contains the string on which the operation is to be performed
  • string: the value to be replaced with
  • count (optional): the parameter specifies the maximum number of matches that the method should replace. The default value is 0
  • flags (optional): the regex flags that change the default behavior of the pattern
Python Replace String with Regular Expression Example
import re

str = '[email protected]'

print(re.sub('[a-z]*@', 'ReqBin@', str))
      
# output: [email protected]

Replacing multiple strings with Regular Expressions

The following is an example of replacing multiple patterns at the same time using a regular expression:

Python Replace Multiple Strings with Regular Expression Example
import re

str = '[email protected] [email protected] [email protected]'

print(re.sub('[a-z]*@', 'ReqBin@', str))
      
# output: [email protected] [email protected] [email protected]

See also