Remove first occurrence of character from String in Python

avatar

Borislav Hadzhiev

Last updated: Aug 14, 2022

banner

Photo from Unsplash

Remove first occurrence of character from String in Python #

Use the str.replace() method to remove the first occurrence of a character from a string, e.g. result = my_str.replace('p', '', 1). The replace method will remove the first occurrence of the character from the string by replacing it with an empty string.

main.py
# ✅ Remove first occurrence of character from string my_str = 'apple' result = my_str.replace('p', '', 1) print(result) # 👉️ 'aple' # --------------------------- # ✅ Remove first occurrence of word from string my_str_2 = 'abc one abc two' result_2 = my_str_2.replace('abc ', '', 1) print(result_2) # 👉️ 'one abc two'

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:

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.

We want to remove the first occurrence of the character, so we used an empty string as the replacement.
main.py
my_str = 'apple' result = my_str.replace('p', '', 1) print(result) # 👉️ 'aple'
We specified 1 for the count argument because we only want to replace the first occurrence of the character with an empty string.

You can use the same approach to remove the first occurrence of a word from a string.

main.py
my_str = 'abc one abc two' result = my_str.replace('abc ', '', 1) print(result) # 👉️ 'one abc two'

Alternatively, you can use string slicing.

To remove the first occurrence of a character from a string:

  1. Get the index of the first occurrence of the character.
  2. Get a slice of the string up to the character.
  3. Get a slice of the string after the character.
  4. Concatenate the two strings.
main.py
my_str = 'apple' def remove_first_occurrence(string, char): idx = string.index(char) return string[:idx] + string[idx+len(char):] # 👇️ 'aple' print(remove_first_occurrence('apple', 'p')) # 👇️ '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.

The syntax for string slicing is 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.

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.