Remove everything before a character in a string in Python

avatar

Borislav Hadzhiev

Last updated: Jul 7, 2022

banner

Check out my new book

Remove everything before a character in a string in Python #

To remove everything before a character in a string:

  1. Use the str.find() method to get the index of the character.
  2. Use string slicing and set the start index to the index of the character.
  3. The new string won't contain the preceding characters.
main.py
my_str = 'apple, banana' # ✅ remove everything before character using str.find() result = my_str[my_str.find('b'):] print(result) # 👉️ banana # --------------------------------------------------------- # ✅ Use str.rfind() if you need to remove everything # before last occurrence of character my_str = 'apple,banana,bear' # 👇️ 'bear' print(my_str[my_str.rfind('b'):]) # 👈️ uses str.rfind # 👇️ 'banana,bear' print(my_str[my_str.find('b'):]) # 👈️ uses str.find

The str.find method returns the index of the first occurrence of the provided substring in the string.

We used string slicing to get a part of the original string that starts at the index of the character and continues to the end of the string.

Note that the str.find() method returns -1 if the substring is not found in the string.

You can handle the scenario where the find() method returns -1 in an if/else statement.

main.py
my_str = 'apple, banana' index = my_str.find('b') print(index) # 👉️ 7 if index != -1: result = my_str[index:] else: result = my_str # 👉️ alternatively raise error print(result) # 👉️ 'banana'

And here is an example of the case where the provided character is not in the string.

main.py
my_str = 'apple, banana' index = my_str.find('z') print(index) # 👉️ -1 if index != -1: result = my_str[index:] else: result = my_str # 👉️ alternatively raise error print(result) # 👉️ 'apple, banana'

Our else statement assigns the result variable to the entire string, however you could also raise an exception.

main.py
my_str = 'apple, banana' index = my_str.find('z') print(index) # 👉️ -1 if index != -1: result = my_str[index:] else: # 👇️ this runs raise IndexError('provided character not in string')
If you need to remove everything before last occurrence of a character, use the str.rfind() method.
main.py
my_str = 'apple,banana,bear' result = my_str[my_str.rfind('b'):] print(result) # 👉️ 'bear'

The str.rfind method returns the highest index in the string where the provided substring is found.

The method returns -1 if the substring is not contained in the string.

You can handle the scenario where the character is not present in the string with an if/else statement.

main.py
my_str = 'apple,banana,bear' index = my_str.rfind('b') if index != -1: result = my_str[index:] else: result = my_str print(result) # 👉️ 'bear'

If the else block runs, we set the result variable to the entire string.

Alternatively, you can raise an error in the else block, e.g. raise IndexError('your message here').

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.