Replace the first occurrence of Substring in String - Python

avatar

Borislav Hadzhiev

Last updated: Sep 23, 2022

banner

Photo from Unsplash

Replace the first occurrence of Substring in String - Python #

Use the str.replace() method to replace the first occurrence of a substring in a string, e.g. new_str = my_str.replace('old', 'new', 1). The str.replace() method can be passed a count argument of 1 to only replace the first occurrence of the substring in the string.

main.py
my_str = 'second second third' new_str = my_str.replace('second', 'first', 1) print(new_str) # 👉️ first second third
We set the count argument of the str.replace() method to 1 to only replace the first occurrence of the substring in the string.

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

The method doesn't change the original string. Strings are immutable in Python.

If the substring is not contained in the string, the string is returned as is.

main.py
my_str = 'bobbyhadz.com' new_str = my_str.replace('second', 'first', 1) print(new_str) # 👉️ bobbyhadz.com

If you need to remove the first occurrence of a substring in a string, use an empty string as the replacement.

main.py
my_str = 'oneonetwo' new_str = my_str.replace('one', '', 1) print(new_str) # 👉️ onetwo

Alternatively, you can use the re.sub() method.

Replace the first occurrence of Substring in String using re.sub() #

Use the re.sub() method to replace the first occurrence of a substring in a string, e.g. new_str = re.sub('old', 'new', my_str, 1). The re.sub() method can be passed a count argument of 1 to only replace the first occurrence of the substring in the string.

main.py
import re my_str = 'second second third' new_str = re.sub('second', 'first', my_str, 1) print(new_str) # 👉️ first second third

The re.sub method returns a new string that is obtained by replacing the occurrences of the pattern with the provided replacement.

If the pattern isn't found, the string is returned as is.

main.py
import re my_str = 'bobbyhadz.com' new_str = re.sub('second', 'first', my_str, 1) print(new_str) # 👉️ bobbyhadz.com
The first argument we passed to the re.sub() method is the pattern we want to match in the string and the second is the replacement for the match.

Alternatively, you can use the str.split() and str.join() methods.

Replace the first occurrence of Substring in String using str.split() #

To replace the first occurrence of a substring in a string:

  1. Use the str.split() method to split the string on the substring once.
  2. Use the str.join() method to join the list into a string with the provided replacement.
main.py
my_str = 'second second third' new_str = 'first'.join(my_str.split('second', 1)) print(new_str) # 👉️ first second third

We used the str.split() method to split the string on the substring we want to replace, once.

main.py
my_str = 'second second third' # 👇️ ['', ' second third'] print(my_str.split('second', 1))

The str.split() method splits the string into a list of substrings using a delimiter.

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)

The last step is to use the str.join() method to join the list into a string with the provided replacement string as the separator.

main.py
my_str = 'second second third' new_str = 'first'.join(my_str.split('second', 1)) print(new_str) # 👉️ first second third

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

If the substring is not found in the string, the string is returned as is.

main.py
my_str = 'bobbyhadz.com' new_str = 'first'.join(my_str.split('second', 1)) print(new_str) # 👉️ bobbyhadz.com
I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.