Change language

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’] 

Python | Methods for splitting strings into capital letters Python functions: Questions

Python | Methods for splitting strings into capital letters split: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method