Skip to main content

How to Remove Parts of Strings Before or After a Character in Python

This guide explains how to remove portions of a string in Python, specifically:

  • Removing everything before a specific character (or the last occurrence of that character).
  • Removing everything after a specific character (or the last occurrence of that character).

We will cover methods like split(), rsplit(), partition(), rpartition(), find(), rfind() and string slicing, showing the best approach for each situation.

Removing Everything After a Character (First Occurrence)

The split() method is the most straightforward and readable way to remove everything after the first occurrence of a character:

my_str = 'tutorial!reference!com'
separator = '!'
result = my_str.split(separator, 1)[0] # Split at most once
print(result) # Output: tutorial
  • my_str.split(separator, 1):
    • separator: The character to split on.
    • 1: This is the maxsplit argument. It limits the number of splits to 1. This is crucial for only splitting at the first occurrence. Without it, you'd split at every occurrence.
  • [0]: Selects the first element of the resulting list (the part before the separator).
  • If the character is not present in the list, the method returns the original string.

To include the delimiter:

my_str = 'tutorial!reference!com'
separator = '!'
result = my_str.split(separator, 1)[0] + separator # Add separator
print(result) # Output: tutorial!

Using partition()

The partition() method splits the string at the first occurrence of the separator and returns a tuple: (part_before, separator, part_after).

my_str = 'tutorial!reference!com'
separator = '!'

result = my_str.partition(separator)[0] # Part before the separator
print(result) # Output: tutorial

#If you want to include the separator:
result = ''.join(my_str.partition(separator)[0:2])
print(result) # Output: tutorial!
  • The partition method returns three elements, and you can use indexes to determine which parts you want to return.
note

This method is slightly less readable than split() for this specific task, but it's useful to know about.

Removing Everything After the Last Occurrence of a Character

To remove everything after the last occurrence, use rsplit() (right split) with maxsplit=1:

my_str = 'tutorial!reference!com'
separator = '!'
result = my_str.rsplit(separator, 1)[0] # Split once from the right
print(result) # Output: tutorial!reference
  • To also include the last occurrence, you have to concatenate with the separator:

    result = my_str.rsplit(separator, 1)[0] + separator
  • my_str.rsplit(separator, 1): Splits the string at the separator, but does at most one split, starting from the right.

  • [0]: Selects the first element of the resulting list (everything before the last separator).

Using rpartition()

rpartition() splits the string at the last occurrence of the separator and returns a tuple: (part_before, separator, part_after).

my_str = 'tutorial!reference!com'
separator = '!'
result = my_str.rpartition(separator)[0] # part before the last separator.
print(result) # Output: tutorial!reference
  • If you want to include the separator:
result = ''.join(my_str.rpartition(separator)[0:2]) # Joining to include separator
print(result) # Output: tutorial!reference!

Removing Everything Before a Character (First Occurrence)

To remove everything before a character, use a combination of slicing and find():

my_str = 'apple, banana'
result = my_str[my_str.find('b'):] # start the slicing from the index of 'b'
print(result) # Output: banana
  • Use find() to locate the index of the first occurrence.
  • Use slicing to extract the substring from the found index until the end.
  • If the substring does not exist, you will get -1, and should handle this edge case to avoid slicing errors.
my_str = 'apple, banana'
index = my_str.find('z')
if index != -1:
result = my_str[index:]
else:
result = my_str # Original string

print(result) # Output: apple, banana

Removing Everything Before the Last Occurrence of a Character

To remove everything before the last occurrence of a character, use rfind() and slicing:

my_str = 'apple,banana,bear'
result = my_str[my_str.rfind('b'):] # Using rfind
print(result) # Output: bear
  • rfind() is used to get the index of the last occurrence of the character.
  • If the character is not present in the string, then you get -1, and have to handle this edge case.

You can also use rsplit():

my_str = 'example.com/articles/python'
result = my_str.rsplit('/', 1)[1] # Split from the right.
print(result) # Output: python
  • The rsplit('/', 1) will split the string only once, at the last occurrence of the separator.

You can also use rpartition() to do this:

my_str = 'example.com/articles/python'
result = my_str.rpartition('/')[2] # Access the part after the separator
print(result) # Output: python