Remove everything after a character in a string in Python

avatar

Borislav Hadzhiev

Last updated: Jul 7, 2022

banner

Photo from Unsplash

Remove everything after a character in a string in Python #

To remove everything after a character in a string:

  1. Use the str.split() method to split the string on the separator.
  2. Access the list element at index 0 to get everything before the separator.
  3. Optionally, use the addition (+) operator to add the separator.
main.py
my_str = 'one!two!three' # ✅ remove everything after FIRST occurrence of character separator = '!' result_1 = my_str.split(separator, 1)[0] print(result_1) # 👉️ 'one' # --------------------------------------------------------- # ✅ remove everything after LAST occurrence of character result_2 = my_str.rsplit(separator, 1)[0] print(result_2) # 👉️ 'one!two' # --------------------------------------------------------- # ✅ remove everything after character, keeping separator result_3 = my_str.split(separator, 1)[0] + separator print(result_3) # 👉️ 'one!'

We used the str.split() method to remove everything after a character (! in the examples).

The str.split() method splits the string into a list of substrings using a delimiter.

The method takes the following 2 parameters:

NameDescription
separatorSplit the string into substrings on each occurrence of the separator
maxsplitAt most maxsplit splits are done (optional)

If the separator is not found in the string, a list containing only 1 element is returned.

We set the maxsplit argument to 1 because we only need to split the string once.

The first example removes everything after the first occurrence of the character in the string.

main.py
my_str = 'one!two!three' # ✅ remove everything after FIRST occurrence of character separator = '!' result_1 = my_str.split(separator, 1)[0] print(result_1) # 👉️ 'one' # 👇️ ['one', 'two!three'] print(my_str.split(separator, 1))
Notice that the separator is not included in the string. If you need to include it, use the addition (+) operator.
main.py
my_str = 'one!two!three' # ✅ remove everything after character, keeping separator separator = '!' result_1 = my_str.split(separator, 1)[0] + separator print(result_1) # 👉️ 'one!'

The addition (+) operator can be used to concatenate strings in Python.

If you need to remove everything after the last occurrence of the character in the string, use the str.rsplit() method.

main.py
my_str = 'one!two!three' separator = '!' # ✅ remove everything after LAST occurrence of character result_2 = my_str.rsplit(separator, 1)[0] print(result_2) # 👉️ 'one!two'

Except for splitting from the right, rsplit() behaves like split().

The str.rsplit() method splits the string from the right, and with maxsplit set to 1, it only splits once.

If you need to include the character you split on, use the addition operator (+).

main.py
my_str = 'one!two!three' separator = '!' result_2 = my_str.rsplit(separator, 1)[0] + separator print(result_2) # 👉️ 'one!two!'

You might also use the str.partition() method to remove everything after a specific character in a string.

main.py
my_str = 'one!two!three' separator = '!' result_1 = my_str.partition(separator)[0] print(result_1) # 👉️ 'one' result_2 = ''.join(my_str.partition(separator)[0:2]) print(result_2) # 👉️ 'one!'

The str.partition method splits the string at the first occurrence of the provided separator.

The method returns a tuple containing 3 elements - the part before the separator, the separator, and the part after the separator.

main.py
my_str = 'one!two!three' separator = '!' # 👇️ ('one', '!', 'two!three') print(my_str.partition(separator))
If the separator is not found in the string, the method returns a tuple containing the string, followed by 2 empty strings.

If you need to include the separator in the result, use the str.join() method to join the first and second list items.

main.py
my_str = 'one!two!three' separator = '!' result_2 = ''.join(my_str.partition(separator)[0:2]) print(result_2) # 👉️ 'one!'

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

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.