Python | Methods for splitting strings into capital letters



Method # 1: Using the re.findall () method

# Python code for demonstration
# split lines
# capital letter

 

import re

 
# Initialization string

ini_str = `GeeksForGeeks`

  
# Print start line

print ( "Initial String" , ini_str)

 
# UpperCase split using re

res_list = []

res_list = re.findall ( `[AZ] [^ AZ] *` , ini_str)

 
# Print result

print ( "Resultant prefix" , str (res_list))

Exit:

 Initial String GeeksForGeeks Resultant prefix [`Geeks`,` For`, `Geeks`] 

Method # 2: Using re.split ()

# Python code for demo
# split lines
# capital letter

 

import re

 
# Initializing string

ini_str = `GeeksForGeeks`

 
# Print start line

print ( "Initial String" , ini_str)

 
# UpperCase split using re

res_list = [s for s in re.split ( "([AZ] [^ AZ] *)" , ini_str) if s]

 
# Print result

print ( " Resultant prefix " , str (res_list))

Exit:

 Initial String GeeksForGeeks Resultant prefix [`Geeks `,` For`, `Geeks`] 

Method # 3: Using enumeration

# Code Python for demonstration
# split lines # capital letter

 
# Initializing string

ini_str = `GeeksForGeeks `

  
# Print start line

print ( "Initial String" , ini_str)

  
# UpperCase split using re

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 result

print ( "Resultant prefix" , str (res_list)) 

Exit:

 Initial String GeeksForGeeks Resultant prefix [`Geeks`,` For`, `Geeks`]