Python

Python Substring After Character

In Python, a sequence of several characters is known as a string that contains quotes either single or double. If a user wants to get a specific part from the string object, it’s known as slicing. In simple words, it is the operation of getting a substring of a desired string. Sometimes, we just need to get the string that occurs after the substring. In such a situation, different built-in methods of Python can be utilized.

This post will explain different methods for getting a substring after the character in Python.

How to Get Substring After Character in Python?

To get a substring after a character in Python, multiple methods are available that are used for this particular purpose:

Method 1: Get Substring After Character in Python Using “split()” Method

To split a string onto a substring, the “split()” method can be used. This method returns an object list containing elements.

Example

Initially, declare a string variable and pass a string as shown below:

org_string = "LinuxHint is the World best tutorial website!"

Then, create another string variable that holds the word from onward we want to make a substring:

spl_word = 'best'

Now, use the print statement and display the value of the above-declared variables one by one:

print("My Original String: " + str(org_string))

print("Resultant word: " + str(spl_word))

Call the “split()” method and specify the list element index to which we want to access and make a substring. Then, store it in the “result_string” variable:

result_string = org_string.split(spl_word)[1]

Call the “print()” function and pass a “result_string” variable as parameter:

print("Substring After Character : " + result_string)

Output

Method 2: Get Substring After Character in Python Using “partition()” Method

Use the “partition()” method to create a substring after the character. It first looks for a particular string and divides it into three components in a tuple. The element before the supplied string is included in the first element. On the other hand, the specified string is contained in the second element.

Example

Call the “partition()” method with a string value as an argument and specify the desired element index. Then, store it in the declared “result_string” variable:

result_string = org_string.partition(spl_word)[2]

Use the “print()” method to get the desired result:

print("Substring After Character : " + result_string)

As you can see, the provided string is divided into substring after the specified word:

Method 3: Get Substring After Character in Python Using “index()” Method

Another efficient way of substring after the character in Python, the “index()” method can be used. It returns the index of the first occurrence of the specified substring in the desired string.

Example

Use the “index()” method and take the string as a parameter with the specified string length:

result_string = org_string[org_string.index(spl_word) + len(spl_word):]

To display the output of the “result_string” variable on screen, use the print statement:

print("Substring After Character : " + result_string)

Output

Method 4: Substring After Character in Python Utilizing “find()” Method

The “find()” method can also be used for creating a substring from the desired string. It can find the desired character from the string with its length and then pass them to the declared variable.

Example

Call the “find()” with the required split character and its length:

result_string = org_string[org_string.find(spl_word) + len(spl_word):]

Then, view the result of the “find()” method by using the print statement:

print("Substring After Character : " + result_string)

Output

That’s all! We have explained the different methods for substring after the character in Python.

Conclusion

To substring after a character in Python, multiple methods are used in Python, such as the “split()” method, the “partition()” method, the “index()” and the “find()” method. All methods first search the provided word from the given string then divide the string into parts and make a substring. This post elaborated on substring after the character in Python.

About the author

Maria Naz

I hold a master's degree in computer science. I am passionate about my work, exploring new technologies, learning programming languages, and I love to share my knowledge with the world.