Python | Split string on Kth Occurrence of Character
Many problems of split has been dealt with in past, but sometimes, one can also encounter this problem in which we need to split the string on the Kth occurrence of character. Let’s discuss certain ways in which this problem can be solved.
Method #1 : Using split() + join()
The split and join method can perform this particular task. In this, we split the string on basis of character and according to K, we perform a rejoin using the nested join method.
# Python3 code to demonstrate working of # Split string on Kth Occurrence of Character # Using split() + join() # initializing string test_str = "Geeks_for_Geeks_is_best" # split char splt_char = "_" # initializing K K = 3 # printing original string print("The original string is : " + str(test_str)) # Using split() + join() # Split string on Kth Occurrence of Character temp = test_str.split(splt_char) res = splt_char.join(temp[:K]), splt_char.join(temp[K:]) # printing result print("Is list after Kth split is : " + str(list(res))) |
The original string is : Geeks_for_Geeks_is_best Is list after Kth split is : ['Geeks_for_Geeks', 'is_best']
Method #2 : Using regex
This problem can also be solved using the regex to perform this particular task. Along with slicing, the finditer method of regex library can help to achieve this particular task.
# Python3 code to demonstrate working of # Split string on Kth Occurrence of Character # Using regex import re # initializing string test_str = "Geeks_for_Geeks_is_best" # split char splt_char = "_" # initializing K K = 3 # printing original string print("The original string is : " + str(test_str)) # Using regex # Split string on Kth Occurrence of Character temp = [x.start() for x in re.finditer(splt_char, test_str)] res1 = test_str[0:temp[K - 1]] res2 = test_str[temp[K - 1] + 1:] res = (res1 + " " + res2).split(" ") # printing result print("Is list after Kth split is : " + str(res)) |
The original string is : Geeks_for_Geeks_is_best Is list after Kth split is : ['Geeks_for_Geeks', 'is_best']
Recommended Posts:
- 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 | Split on last occurrence of delimiter
- Python | Replace multiple occurrence of character by single
- Python | Get the string after occurrence of given substring
- Python | Ways to find nth occurrence of substring in a string
- Python String | split()
- How to split a string in C/C++, Python and Java?
- Python | Split string into list of characters
- Python | String Split including spaces
- Python | Split given string into equal halves
- Python program to split and join a string
- Split a string in equal parts (grouper in Python)
- Python | Split string in groups of n consecutive characters
- Python | Split CamelCase string to individual strings
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.



