Python | Get the string after occurrence of given substring
Sometimes, more than finding a substring, we might need to get the string which is occurring after the substring has been found. Let’s discuss certain ways in which this task can be performed.
Method #1 : Using partition()
The partition function can be used to perform this task in which we just return the part of partition occurring after the partition word.
# Python3 code to demonstrate # Get String after substring occurrence # using partition() # initializing string test_string = "GeeksforGeeks is best for geeks" # initializing split word spl_word = 'best' # printing original string print("The original string : " + str(test_string)) # printing split string print("The split string : " + str(spl_word)) # using partition() # Get String after substring occurrence res = test_string.partition(spl_word)[2] # print result print("String after the substring occurrence : " + res) |
The original string : GeeksforGeeks is best for geeks The split string : best String after the substring occurrence : for geeks
Method #2 : Using split()
The split function can also be applied to perform this particular task, in this function, we use the power of limiting the split and then print the later string.
# Python3 code to demonstrate # Get String after substring occurrence # using split() # initializing string test_string = "GeeksforGeeks is best for geeks" # initializing split word spl_word = 'best' # printing original string print("The original string : " + str(test_string)) # printing split string print("The split string : " + str(spl_word)) # using split() # Get String after substring occurrence res = test_string.partition(spl_word)[2] # print result print("String after the substring occurrence : " + res) |
The original string : GeeksforGeeks is best for geeks The split string : best String after the substring occurrence : for geeks
Recommended Posts:
- Python | Ways to find nth occurrence of substring in a string
- Python | Find last occurrence of substring
- Python | Split string on Kth Occurrence of Character
- Python program to find occurrence to each character in given string
- Generate two output strings depending upon occurrence of character in input string in Python
- Python | Frequency of substring in given string
- Python | Remove the given substring from end of string
- Python | All occurrences of substring in string
- Python | Check if a Substring is Present in a Given String
- Python | Count overlapping substring in a given string
- Python | Get the substring from given string using list slicing
- Python | Check if substring present in string
- Python | Ways to count number of substring in string
- Python | Add the occurrence of each number as sublists
- Python | Split on last occurrence of delimiter
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



