Get the string after a specific substring in Python

avatar

Borislav Hadzhiev

Last updated: Sep 23, 2022

banner

Photo from Unsplash

Get the string after a specific substring in Python #

To get the string after a specific substring:

  1. Use the str.split() method to split the string once on the substring.
  2. Access the list element at index 1.
  3. The list element at index 1 contains the string after the specific substring.
main.py
my_str = 'one two two three' result = my_str.split('two ', 1)[1] print(result) # 👉️ two three

We called the str.split() method with a count argument set to 1, so the string is only split once.

main.py
my_str = 'one two two three' print(my_str.split('two ', 1)) # 👉️ ['one ', 'two three'] print(my_str.split('two ')) # 👉️ ['one ', '', 'three']

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)

To get everything after the substring, we just have to access the list element at index 1.

main.py
my_str = 'one two two three' result = my_str.split('two ', 1)[1] print(result) # 👉️ two three
If you need to handle the scenario where the string doesn't contain the substring, use the ternary operator.
main.py
my_str = 'one two two three' substring = 'abc' result = (my_str.split(substring, 1)[1] if substring in my_str else my_str) print(result) # 👉️ one two two three

If the substring is contained in the string, we return the part after the substring.

Otherwise, we return the string as is.

Depending on your use case, you might also want to return an empty string if the substring is not contained in the string.

main.py
my_str = 'one two two three' substring = 'abc' result = (my_str.split(substring, 1)[1] if substring in my_str else '') print(result) # 👉️ ""

Alternatively, you can use string slicing.

Get the string after a specific substring using string slicing #

To get the string after a specific substring:

  1. Use the str.index() method to get the index of the substring.
  2. Add the length of the substring to the index.
  3. Use string slicing to return the string after the specified substring.
main.py
my_str = 'one two two three' substring = 'two ' result = my_str[my_str.index(substring) + len(substring):] print(result) # 👉️ two three

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

We added the length of the substring to the index to start the string slice immediately after the substring.

The syntax for string slicing is my_str[start:stop:step].

The start index is inclusive, whereas the stop index is exclusive (up to, but not including).

The str.index() method raises a ValueError if the substring is not found in the string.

You can use the ternary operator if you need to handle this scenario.

main.py
my_str = 'one two two three' substring = 'abc' result = (my_str[my_str.index(substring) + len(substring):] if substring in my_str else my_str) print(result) # 👉️ one two two three

If the substring is found in the string, we return the slice after the substring.

Otherwise, we return the string as is.

Alternatively, you can use the str.partition() method.

Get the string after a specific substring using str.partition() #

To get the string after a specific substring:

  1. Use the str.partition() method to split the string on the first occurrence of the substring.
  2. Access the list element at index 2.
  3. The list element at index 2 contains the string after the specific substring.
main.py
my_str = 'one two two three' result = my_str.partition('two ')[2] print(result) # 👉️ two three

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

main.py
my_str = 'bobby!hadz!com' separator = '!' # 👇️ ('bobby', '!', 'hadz!com') print(my_str.partition(separator))

The method returns a tuple containing 3 elements - the part before the separator, the separator, and the part after the 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 handle the scenario where the substring is not contained in the string, use the ternary operator.

main.py
my_str = 'one two two three' substring = 'abc' result = (my_str.partition(substring)[2] if substring in my_str else my_str) print(result) # 👉️ two three

If the substring is contained in the string, we return the result of calling the str.partition() method, otherwise, we return the string as is.

Which approach you pick is a matter of personal preference. I'd go with using the str.split() method because I find it quite intuitive and easy to read.

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.