Last updated: Apr 9, 2024
Reading timeยท3 min
Use the str.replace()
method to remove the first occurrence of a character
from a string.
The replace
method will remove the first occurrence of the character from
the string by replacing it with an empty string.
my_str = 'bobbyhadz' result = my_str.replace('b', '', 1) print(result) # ๐๏ธ 'obbyhadz'
We used the str.replace()
method to remove the first occurrence of a character
from a string.
The str.replace() method returns a copy of the string with N occurrences of a substring replaced by the provided replacement.
The method takes the following parameters:
Name | Description |
---|---|
old | The substring we want to replace in the string |
new | The replacement for each occurrence of old |
count | Only the first count occurrences are replaced (optional) |
The method doesn't change the original string. Strings are immutable in Python.
We want to remove the first occurrence of the character, so we used an empty string as the replacement.
my_str = 'bobbyhadz' result = my_str.replace('b', '', 1) print(result) # ๐๏ธ 'obbyhadz'
1
for the count
argument because we only want to remove the first occurrence of the character.You can use the same approach to remove the first occurrence of a word from a string.
my_str = 'abc one abc two' result = my_str.replace('abc ', '', 1) print(result) # ๐๏ธ 'one abc two'
Alternatively, you can use string slicing.
This is a four-step process:
def remove_first_occurrence(string, char): idx = string.index(char) return string[:idx] + string[idx+len(char):] # ๐๏ธ 'obbyhadz' print(remove_first_occurrence('bobbyhadz', 'b')) # ๐๏ธ 'one abc two' print(remove_first_occurrence('abc one abc two', 'abc '))
We defined a reusable function that takes a string and a character (or a word) and removes the first occurrence of the character (or word) from the string.
my_str[start:stop:step]
where the start
index is inclusive and the stop
index is exclusive.The slice string[:idx]
starts at the beginning of the string and goes up to,
but not including the character.
The slice string[idx+len(char):]
starts at the index after the character and
goes to the end of the string.
The start
index is inclusive, so we added the length of the substring to omit
it from the result.
This approach can also be used to remove the first occurrence of a word from a string.
You can also use the re.sub()
method to remove the first occurrence of a
character in a string.
import re my_str = 'bobbyhadz' result = re.sub(r'b', '', my_str, count=1) print(result) # ๐๏ธ 'obbyhadz'
The re.sub() method returns a new string that is obtained by replacing the occurrences of the pattern with the provided replacement.
import re a_str = 'bobby,hadz.com;' result = re.sub(r'[^\w\s]', '_', a_str) print(result) # ๐๏ธ bobby_hadz_com_
If the pattern isn't found, the string is returned as is.
We didn't have to use a regular expression in the example, but you can specify
one as the first argument of re.sub()
if your use case requires that.
You can learn more about the related topics by checking out the following tutorials: