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
# Python code to demonstrate# to split strings# on uppercase letterimport re# Initialising stringini_str = 'GeeksForGeeks'# Printing Initial stringprint ("Initial String", ini_str)# Splitting on UpperCase using reres_list = []res_list = re.findall('[A-Z][^A-Z]*', ini_str)# Printing resultprint("Resultant prefix", str(res_list)) |
Output:
Initial String GeeksForGeeks Resultant prefix ['Geeks', 'For', 'Geeks']
Method #2: Using re.split()
Python3
# Python code to demonstrate# to split strings# on uppercase letterimport re# Initialising stringini_str = 'GeeksForGeeks'# Printing Initial stringprint ("Initial String", ini_str)# Splitting on UpperCase using reres_list = [s for s in re.split("([A-Z][^A-Z]*)", ini_str) if s]# Printing resultprint("Resultant prefix", str(res_list)) |
Output:
Initial String GeeksForGeeks Resultant prefix ['Geeks', 'For', 'Geeks']
Method #3: Using enumerate
Python3
# Python code to demonstrate# to split strings# on uppercase letter# Initialising stringini_str = 'GeeksForGeeks'# Printing Initial stringprint ("Initial String", ini_str)# Splitting on UpperCaseres_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 resultprint("Resultant prefix", str(res_list)) |
Output:
Initial String GeeksForGeeks Resultant prefix ['Geeks', 'For', 'Geeks']
Method #4 : Using isupper() and split() methods
Python3
# Python code to demonstrate# to split strings# on uppercase letter# Initialising stringini_str = 'GeeksForGeeks'# Printing Initial stringprint ("Initial String", ini_str)# Splitting on UpperCaseres=""for i in ini_str: if(i.isupper()): res+="*"+i else: res+=ix=res.split("*")x.remove('')# Printing resultprint("Resultant prefix", str(x)) |
Output
Initial String GeeksForGeeks Resultant prefix ['Geeks', 'For', 'Geeks']
Method #5 : Using ord() function
Python3
# Python code to demonstrate# to split strings# on uppercase letter# Initialising stringini_str = 'GeeksForGeeks'# Printing Initial stringprint ("Initial String", ini_str)# Splitting on UpperCaseres=""for i in ini_str: if(ord(i) in range(65,91)): res+="*"+i else: res+=ix=res.split("*")x.remove('')# Printing resultprint("Resultant prefix", str(x)) |
Output
Initial String GeeksForGeeks Resultant prefix ['Geeks', 'For', 'Geeks']


