Remove first occurrence of character from String in Python

avatar
Borislav Hadzhiev

Last updated: Feb 20, 2023
3 min

banner

# Table of Contents

  1. Remove first occurrence of character from String in Python
  2. Remove first occurrence of a Word from a String in Python
  3. Remove first occurrence of character from String using string slicing
  4. Remove first occurrence of character from String using re.sub()

# 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.

The replace method will remove the first occurrence of the character from the string by replacing it with an empty string.

main.py
my_str = 'bobbyhadz' result = my_str.replace('b', '', 1) print(result) # ๐Ÿ‘‰๏ธ 'obbyhadz'

remove first occurrence of character from string

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 = 'bobbyhadz' result = my_str.replace('b', '', 1) print(result) # ๐Ÿ‘‰๏ธ 'obbyhadz'
We specified 1 for the count argument because we only want to remove the first occurrence of the character.

# Remove first occurrence of a Word from a String in Python

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'

remove first occurrence of word from string

Alternatively, you can use string slicing.

# Remove first occurrence of character from String using string slicing

This is a four-step process:

  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
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 '))

remove first occurrence character from string using slicing

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.

# Remove first occurrence of character from String using re.sub()

You can also use the re.sub() method to remove the first occurrence of a character in a string.

main.py
import re my_str = 'bobbyhadz' result = re.sub(r'b', '', my_str, count=1) print(result) # ๐Ÿ‘‰๏ธ 'obbyhadz'

remove first occurrence of character using re sub

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

main.py
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev