Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Python | Split string on Kth Occurrence of Character

  • Last Updated : 17 Jun, 2021

Many problems of the split have been dealt with in the past, but sometimes, one can also encounter this problem in which we need to split the string on the Kth occurrence of a 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. 
 

 Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.  

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course

Python3




# 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)))
Output



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




# 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))
Output
The original string is : Geeks_for_Geeks_is_best
Is list after Kth split is : ['Geeks_for_Geeks', 'is_best']

Method #3: Using maxsplit from split  

You can use the split itself to get the kth number the second parameter of the method split, that is the max split, then you have your string.  

Probably the easiest way

Python3




test_str = "Geeks for geeks"
 
K = 1 #kth occurrence
 
new_str, rest = test_str.split(" ", K)
                                
#or if you want a list
                                
new_str_list = test_str.split(" ", K)
 
print(new_str)
print(rest)
 
print(f'list: {new_str_list}')
Output
Geeks
for geeks
list: ['Geeks', 'for geeks']



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!