Python | Ways to split strings on Uppercase characters
Given a string, write a Python program to split strings on Uppercase characters. Let’s discuss a few methods to solve the problem.
Method #1: Using re.findall() method
Python3
import re
ini_str = 'GeeksForGeeks'
print ( "Initial String" , ini_str)
res_list = []
res_list = re.findall( '[A-Z][^A-Z]*' , ini_str)
print ( "Resultant prefix" , str (res_list))
|
Output:
Initial String GeeksForGeeks
Resultant prefix ['Geeks', 'For', 'Geeks']
Time Complexity: O(n), where n is the length of the given string.
Auxiliary Space: O(n)
Method #2: Using re.split()
Python3
import re
ini_str = 'GeeksForGeeks'
print ( "Initial String" , ini_str)
res_list = [s for s in re.split( "([A-Z][^A-Z]*)" , ini_str) if s]
print ( "Resultant prefix" , str (res_list))
|
Output:
Initial String GeeksForGeeks
Resultant prefix ['Geeks', 'For', 'Geeks']
Time complexity of this code is O(n), where n is the length of the initial string “ini_str”. This is because the “re.split” function is used to split the string, which has a linear time complexity.
Auxiliary space used by this code is O(n), where n is the length of the resultant prefix list “res_list”. This is because the “res_list” list is created by splitting the initial string, which requires additional space to store the elements of the list.
Method #3: Using enumerate
Python3
ini_str = 'GeeksForGeeks'
print ( "Initial String" , ini_str)
res_pos = [i for i, e in enumerate (ini_str + 'A' ) if e.isupper()]
res_list = [ini_str[res_pos[j]:res_pos[j + 1 ]]
for j in range ( len (res_pos) - 1 )]
print ( "Resultant prefix" , str (res_list))
|
Output:
Initial String GeeksForGeeks
Resultant prefix ['Geeks', 'For', 'Geeks']
Time complexity: O(n), where n is the length of the initial string. The time complexity is dominated by the loop that splits the string into substrings based on uppercase letters.
Auxiliary space: O(n), where n is the length of the initial string. The auxiliary space is used to store the result list of substrings.
Method #4 : Using isupper() and split() methods
Python3
ini_str = 'GeeksForGeeks'
print ( "Initial String" , ini_str)
res = ""
for i in ini_str:
if (i.isupper()):
res + = "*" + i
else :
res + = i
x = res.split( "*" )
x.remove('')
print ( "Resultant prefix" , str (x))
|
Output
Initial String GeeksForGeeks
Resultant prefix ['Geeks', 'For', 'Geeks']
Time complexity : O(n), where n is length of ini_str string.
Auxiliary space : O(n), where n is length of res string.
Method #5 : Using ord() function
Python3
ini_str = 'GeeksForGeeks'
print ( "Initial String" , ini_str)
res = ""
for i in ini_str:
if ( ord (i) in range ( 65 , 91 )):
res + = "*" + i
else :
res + = i
x = res.split( "*" )
x.remove('')
print ( "Resultant prefix" , str (x))
|
Output
Initial String GeeksForGeeks
Resultant prefix ['Geeks', 'For', 'Geeks']
Method #6: Without using builtin methods(isupper() and ord())
Python3
ini_str = 'GeeksForGeeks'
print ( "Initial String" , ini_str)
upperalphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
res = ""
for i in ini_str:
if i in upperalphabets:
res + = "*" + i
else :
res + = i
x = res.split( "*" )
x.remove('')
print ( "Resultant prefix" , str (x))
|
Output
Initial String GeeksForGeeks
Resultant prefix ['Geeks', 'For', 'Geeks']
Method #7 : Using operator.countOf() method
Python3
import operator as op
ini_str = 'GeeksForGeeks'
print ( "Initial String" , ini_str)
upperalphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
res = ""
for i in ini_str:
if op.countOf(upperalphabets, i) > 0 :
res + = "*" + i
else :
res + = i
x = res.split( "*" )
x.remove('')
print ( "Resultant prefix" , str (x))
|
Output
Initial String GeeksForGeeks
Resultant prefix ['Geeks', 'For', 'Geeks']
Time Complexity: O(n)
Space Complexity: O(n)
Last Updated :
25 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...