In this Python tutorial, we will learn how to get a string after a specific substring.
Table Of Contents
Get a string after a specific substring using split()
Here, we will use the split() method to fetch the part of a string after a specific substring.
Syntax:
string.split(substring, 1)[1]
Here, the string is the actual string object that will be split. Substring will act as delimeter and a list of 2 elements will be returned. First in list will be a substring before the delimeter and second element will be a substring after the delimeter. We will select the second element from the returned list.
Example 1:
# Consider the string string1="Python" print("Original String: ", string1) # Get the string after "th" subString = string1.split("th", 1)[1] print("String after substring \"th\" : ", subString)
Output:
Original String: Python String after substring "th" : on
We specified substring as “th”. So after substring, the remaining string is returned.
Example 2:
# Consider the string string1="Welcome to thisPointer" print("Original String: ",string1) # Get the string after "to" print("String after substring-to : ",string1.split("to", 1)[1]) # Get the string after "Welcome" print("String after substring-Welcome : ",string1.split("Welcome", 1)[1]) # Get the string after "this" print("String after substring-this : ",string1.split("this", 1)[1])
Output:
Original String: Welcome to thisPointer String after substring-to : thisPointer String after substring-Welcome : to thisPointer String after substring-this : Pointer
We specified substring as
1. “to” – so the remaining string after removing this substring is “thisPointer”
2. “Welcome” – so the remaining string after removing this substring is ” to thisPointer”
3. “this” – so the remaining string after removing this substring is “Pointer”.
Get a string after a specific substring using slicing
Here, we will apply the slicing operation to get the string after a particular substring. For that, we will slice the where the substring ends. As the start position in slicing, specify the index of substring plus the length of the substring and keep the end position of slicing as empty. So it will select the remaining string after the substring.
Syntax:
string[string.index(substring) + len(substring):]
Here, the string is the actual string object from which the entire string is removed before the substring.
Example 1:
# Consider the string string1="Python" print("Original String: ",string1) # Get the string after "th" subString = string1[string1.index("th") + len("th"):] print("String after substring-th : ", subString)
Output:
Original String: Python String after substring-th : on
We specified substring as “th”. So after substring -th, the remaining string is returned.
Example 2:
# Consider the string string1="Welcome to thisPointer" print("Original String: ",string1) # Get the string after "to" print("String after substring-to : ",string1[string1.index("to") + len("to"):]) # Get the string after "Welcome" print("String after substring-Welcome : ",string1[string1.index("Welcome") + len("Welcome"):]) # Get the string after "this" print("String after substring-this : ",string1[string1.index("this") + len("this"):])
Output:
Original String: Welcome to thisPointer String after substring-to : thisPointer String after substring-Welcome : to thisPointer String after substring-this : Pointer
We specified substring as
- “to” – so the remaining string after removing this substring is “thisPointer”
- “Welcome” – so the remaining string after removing this substring is ” to thisPointer”
- “this” – so the remaining string after removing this substring is “Pointer”.
Get a string after a specific substring using partition()
Here, we will use the partition() function to partition the string at the substring.
Syntax:
string.partition(substring)[2]
Here, the string is the actual string object and the entire string is removed before the substring.
Example 1:
# Consider the string string1="Python" print("Original String: ",string1) # Get the string after "th" subString = string1.partition("th")[2] print("String after substring-th : ", subString)
Output:
Original String: Python String after substring-th : on
We specified substring as “th”. So after substring -th, the remaining string is returned.
Example 2:
# Consider the string string1="Welcome to thisPointer" print("Original String: ",string1) # Get the string after "to" print("String after substring-to : ",string1.partition("to")[2]) # Get the string after "Welcome" print("String after substring-Welcome : ",string1.partition("Welcome")[2]) # Get the string after "this" print("String after substring-this : ",string1.partition("this")[2])
Output:
Original String: Welcome to thisPointer String after substring-to : thisPointer String after substring-Welcome : to thisPointer String after substring-this : Pointer
We specified substring as
- “to” – so the remaining string after removing this substring is “thisPointer”
- “Welcome” – so the remaining string after removing this substring is ” to thisPointer”
- “this” – so the remaining string after removing this substring is “Pointer”.
Summary
In this article, we learned to get a string after a specific substring using split(), slicing, and partition() methods. In all the cases, we have to specify the substring. Thanks.