3 different Python programs to get a string after a substring

Python program to get a string after a substring:

In this post, we will learn how to find a string after the first occurrence of a substring. This post will show you three different ways to solve it.

Method 1: By using split:

The split method can be used to get a list of words from a string by using a separator. This method is defined as below:

str.split(sep, maxSplit)

Here, sep is the separator and maxSplit is the number of splits to do. The maxSplit is an optional value and it is -1 by default. It returns maxSplit + 1 number of elements.

To get a string after a substring, we need to pass the substring as the first parameter and 1 as the second parameter. It will return a list of 2 words. The second word will be the required string after the substring.

Let me show this with an example:

given_str = 'The quick brown fox jumps over the lazy dog'

print(given_str.split('brown', 1)[1])

It will print the string after the word brown in the string given_str.

$ python3 example.py
 fox jumps over the lazy dog

Let’s try it with different words:

given_str = 'The quick brown fox jumps over the lazy dog'

print('Given string: ',given_str)
print(f"String after 'brown': {given_str.split('brown', 1)[1]}")
print(f"String after 'jumps': {given_str.split('jumps', 1)[1]}")
print(f"String after 'lazy': {given_str.split('lazy', 1)[1]}")
print(f"String after 'dog': {given_str.split('dog', 1)[1]}")
print(f"String after 'quick': {given_str.split('quick', 1)[1]}")

It will print:

$ python3 example.py
Given string:  The quick brown fox jumps over the lazy dog
String after 'brown':  fox jumps over the lazy dog
String after 'jumps':  over the lazy dog
String after 'lazy':  dog
String after 'dog': 
String after 'quick':  brown fox jumps over the lazy dog

Method 2: By using string.partition:

The partition method takes one separator as its parameter and it returns a tuple containing three strings:

  • The substring before the separator
  • The separator itself
  • The substring after the separator

It is defined as:

str.partition(separator)

If the separator is not found in the string, it will return the string as the first item of the tuple and two empty strings.

We can pass the substring as the separator and pick the third element to get the string after the substring.

given_str = 'The quick brown fox jumps over the lazy dog'

print('Given string: ',given_str)
print(f"String after 'brown': {given_str.partition('brown')[2]}")
print(f"String after 'jumps': {given_str.partition('jumps')[2]}")
print(f"String after 'lazy': {given_str.partition('lazy')[2]}")
print(f"String after 'dog': {given_str.partition('dog')[2]}")
print(f"String after 'quick': {given_str.partition('quick')[2]}")

This is the same program we used before. It is changed to use the partition method.

You will get the same output if you run it.

Python string after substring

Method 3: By using string slicing:

Another way to solve this problem is to use string slicing. With string slicing, we can get the substring from a string within a given range. For example, str[start:end] will return the substring in str from the index start to end - 1.

To get a string after a substring, we have to slice from the right of the end index of the substring to the end of the main string.

  • Get the starting index of the substring by using the str.index(substring) method. Add it with the length of the substring, len(substring) to get the index just after the substring.
  • Don’t pass the end value to the array slicing str[start:]. It will return the required string after the substring.

Let’s try it with an example:

given_str = 'The quick brown fox jumps over the lazy dog'

print(given_str[given_str.index("over") + len("over"):])

It will print the string after the word “over”.

 the lazy dog

You might also like: