Python Substring After Character

In Python, a string is a group of characters. These characters are contained either in single or double-quotes. The process of getting a part of a string from a string object is called getting a substring of a string. It’s also known as a slicing procedure.

In Python, the str[0:n] option extracts a substring from a string. We may need to acquire the string that occurs after the substring has been found in addition to finding the substring.

To construct a substring, check if a substring is present, get the index of a substring, and more; Python provides a variety of functions. This work can be accomplished using the partition function, which simply returns the section of the partition that occurs after the partition word. The partition() method splits a string into three pieces based on the first appearance of the delimiter and returns three-component tuples. Until the first occurrence of the delimiter, the string is the first element. The delimiter string is the second element. Let’s have a look at a couple of different methods for achieving this task.

Example 1:

In our first example, we are going to use the partition method. The partition() method looks for a specified string and breaks it into three components in a tuple. The part before the supplied string is included in the 1st element. The provided string is contained in the second element. The component after the string is contained in the third element. This is a simple illustration to help you comprehend how to split the string before the character. partition(‘ ‘) is used to partition the string based on the delimiter. The first member in the tuple formed by the partition is accessed using [0]. The substring is extracted with the following code.

my_str = "test_program"


The result of the above code is here.

Example 2:

Here is another example, a detailed one that explains the partition function. We have initialized the string first. “my_string” is the name of the string in our case. After that, we have initialized the split word. First, we have printed the original string and the split word.  We then performed the partition function to divide the string. This function will get a string after the substring occurrence. After performing the partition function on the initialized string, print the result in the last line of code.

my_string = "Python is one of the most popular programming languages"

split_word = 'most'

print("Original string: " + str(my_string))

print("Split string: " + str(split_word))

res_str = my_string.partition(split_word)[2]

print("String result after the occurrence substring: " + res_str)

After the character, you’ll see the output, which is a substring.

Example 3:

The split function will be used in this example. This function can also be used for this purpose; in this case, we leverage the power of limiting the split and then printing the resultant text.  The split() function returns a list of objects containing elements. Any whitespace character, such as space, t, n, and so on, is used as the default separator. Separator and maxsplit are the two arguments for this function. The delimiter string (optional) is the separator string. Whitespace will be utilized by default if you don’t specify a separator when invoking the function. The separator, simply put, is a character that will be used to separate each variable.

The value of the separator determines the operation of the split on an empty string. If the separator is not stated or specified as None, the output will be a blank list. If the separator is any string, the outcome will be a list with only one element: a blank string. Maxsplit specifies the maximum number of splits, and it is also optional. As a result, the list can only have maxsplit + 1 element. The default value for maxsplit is -1, which indicates there are no limits to how many splits you can make. Refer to the following code. We’ve started by initializing the string. In our case, the string’s name is my string. The split word has now been initialized. The original string and the divided word were printed first. In the last line of code, output the result of the partition function on the initialized string.

my_string = "Python is one of the most popular programming languages"

split_word = 'most'

print("Original string: " + str(my_string))

print("Split string: " + str(split_word))

res_str = my_string.split(split_word)[1]

print("String result after the occurrence substring: " + res_str)

As you can see from the results below, we achieved the same outcome using the split function.


However, in the Python programming language, there are several ways to extract or return a substring. We have shown you how to return a substring in Python using examples in this section. We use certain Python built-in string functions and features to get or discover the string substring. To summarize, we learned how to obtain a substring following a character. You have also learned how to partition and split data to acquire the desired results. Each of these functions is presented with examples to make it easier to implement the concepts.

About the author

Kalsoom Bibi

Hello, I am a freelance writer and usually write for Linux and other technology related content