Python

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"

print(my_str.partition('_')[0])

https://lh3.googleusercontent.com/p1IowcOOKyUhDOl3QiRVA89d1VBGuXPLLMwrlc2HBdMfT6jpqq5caYJWVYHk6HVGGN9tijNYYkc3P57E8xZ5811XHLyblIXZrSER9DnDuykUfCQ1f81T4ELYWw0bR_Ej9Q5zzSf0

The result of the above code is here.

https://lh4.googleusercontent.com/Xm9XqVi_iqAQgGE9Xvktn7-bYVkIqCC0cZgv3uqTVn0SaBQxNI6Vy4-YrjTrXNBRqwoxxp7bJCmO0abNAP7tkAbnhss3pQgDfm16NbzTqXBpHGUjufwhE51Tiz83qfhkHeWQKq1P

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)

https://lh3.googleusercontent.com/C_RKbAWO9n4Z3clV3y9mG2Q5edk0TA_5WZ3X_Els8AZYOLJ9BgaV-G2c4jdB0ui-AQNTOpxXDwr_0i_U454RkgcrDfYd7tdk4SJSNcFTlzuZKzusJ6lkonuHMHCxBvORHs2iBpo-

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

https://lh3.googleusercontent.com/RQHwu8p8ZmlaTUFDq9AgoEdP4PM0l9Y0sUtr5RnV67JBnrdCYQgHGtoZhTh7zaICl-QPyHF3ZteeZhPOxlpWtuqxN119NRF2hd8nCKJjg2DCm6KZKpl7UlNS9uYt0CiK5zas1mYW

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)

https://lh6.googleusercontent.com/qXDuSUIylwn3pVf4Ymuuey0iN1ZaTX3lRttlTCBnQaJqD0WnfCXx08dX2yc65VQ1RFuGYFce9ZTI62W3GfflFTtLxWTWxQvjqd2nrk_3qqjG0t-6w8LM6LGbUQUMAnbq1beAhz_u

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

https://lh3.googleusercontent.com/RQHwu8p8ZmlaTUFDq9AgoEdP4PM0l9Y0sUtr5RnV67JBnrdCYQgHGtoZhTh7zaICl-QPyHF3ZteeZhPOxlpWtuqxN119NRF2hd8nCKJjg2DCm6KZKpl7UlNS9uYt0CiK5zas1mYW

Conclusion:

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