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
# Python code to demonstrate # to split strings # on uppercase letter import re # Initialising string ini_str = 'GeeksForGeeks' # Printing Initial string print ( "Initial String" , ini_str) # Splitting on UpperCase using re res_list = [] res_list = re.findall( '[A-Z][^A-Z]*' , ini_str) # Printing result print ( "Resultant prefix" , str (res_list)) |
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
# Python code to demonstrate # to split strings # on uppercase letter import re # Initialising string ini_str = 'GeeksForGeeks' # Printing Initial string print ( "Initial String" , ini_str) # Splitting on UpperCase using re res_list = [s for s in re.split( "([A-Z][^A-Z]*)" , ini_str) if s] # Printing result print ( "Resultant prefix" , str (res_list)) |
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
# Python code to demonstrate # to split strings # on uppercase letter # Initialising string ini_str = 'GeeksForGeeks' # Printing Initial string print ( "Initial String" , ini_str) # Splitting on UpperCase 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 )] # Printing result print ( "Resultant prefix" , str (res_list)) |
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
# Python code to demonstrate # to split strings # on uppercase letter # Initialising string ini_str = 'GeeksForGeeks' # Printing Initial string print ( "Initial String" , ini_str) # Splitting on UpperCase res = "" for i in ini_str: if (i.isupper()): res + = "*" + i else : res + = i x = res.split( "*" ) x.remove('') # Printing result print ( "Resultant prefix" , str (x)) |
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
# Python code to demonstrate # to split strings # on uppercase letter # Initialising string ini_str = 'GeeksForGeeks' # Printing Initial string print ( "Initial String" , ini_str) # Splitting on UpperCase res = "" for i in ini_str: if ( ord (i) in range ( 65 , 91 )): res + = "*" + i else : res + = i x = res.split( "*" ) x.remove('') # Printing result print ( "Resultant prefix" , str (x)) |
Initial String GeeksForGeeks Resultant prefix ['Geeks', 'For', 'Geeks']
Method #6: Without using builtin methods(isupper() and ord())
Python3
# Python code to demonstrate # to split strings # on uppercase letter # Initialising string ini_str = 'GeeksForGeeks' # Printing Initial string print ( "Initial String" , ini_str) upperalphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # Splitting on UpperCase res = "" for i in ini_str: if i in upperalphabets: res + = "*" + i else : res + = i x = res.split( "*" ) x.remove('') # Printing result print ( "Resultant prefix" , str (x)) |
Initial String GeeksForGeeks Resultant prefix ['Geeks', 'For', 'Geeks']
Method #7 : Using operator.countOf() method
Python3
# Python code to demonstrate # to split strings # on uppercase letter import operator as op # Initialising string ini_str = 'GeeksForGeeks' # Printing Initial string print ( "Initial String" , ini_str) upperalphabets = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" # Splitting on UpperCase res = "" for i in ini_str: if op.countOf(upperalphabets, i) > 0 : res + = "*" + i else : res + = i x = res.split( "*" ) x.remove('') # Printing result print ( "Resultant prefix" , str (x)) |
Initial String GeeksForGeeks Resultant prefix ['Geeks', 'For', 'Geeks']
Time Complexity: O(n)
Space Complexity: O(n)