Change language

Python | Split CamelCase string into separate lines

| | | |

Examples :

  Input:  "GeeksForGeeks"  Output:  [’Geeks’,’ For’, ’ Geeks’]  Input:  "ThisIsInCamelCase"  Output:  [’This’,’ Is’, ’In’,’ Camel’, ’Case’] 

Method # 1: Naive Approach

A naive or crude method to split a CamelCase string into separate lines is to use a for loop. First, use an empty list of "words" and add the first letter "str" ​​to it. Now, using the for loop, check if the current letter is lowercase or not, if yes, add it to the current line, otherwise, if uppercase, start a new separate line.

# Python3 Split Camel Case Program
# line for separate lines

 

def camel_case_split ( str ):

words = [[[ str [ 0 ]]]

 

 < / code> for c in str [ 1 :]:

if words [ - 1 ] [ - 1 ]. islower () and c.isupper ():

words.append ( list (c))

else :

words [ - 1 ]. append (c)

  

  return [’’ .join (word) for word in words]

 
# Driver code

str = "GeeksForGeeks"

print (camel_case_split ( str ))

Exit:

 [’Geeks’,’ For’, ’Geeks’] 

Method # 2: Using enumerate and zip()

In this method, we first use Python enumeration to find indexes where a new line starts and saves We add them to start_idx. Finally, using start_idx, we return every single row using Python zip .

# Python3 Split Camel Case Program
# line for individual lines

 

import re

 

def camel_case_split ( str ) :

 

start_idx = [i for i, e in enumerate ( str )

  if e.isupper ()] + [ len ( str )]

 

start_idx = [ 0 ] + start_idx

return [ str [x: y] for x, y in zip   (start_idx, start_idx [ 1 :])] 

 

 
# Code driver

str = "GeeksForGeeks"

print (camel_case_split ( str ))

Output:

 [’’, ’Geeks’,’ For’, ’Geeks’] 

Method # 3: Using Python Regular Expressions

Exit:

 [’Geeks’,’ For’, ’Geeks’] 

Python | Split CamelCase string into separate lines Python functions: Questions

Python | Split CamelCase string into separate lines Regular Expressions: 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

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

# Python3 Split Camel Case Program
# line for separate lines

import re 

 

def camel_case_split ( str ):

  

return re.findall ( r ’[AZ] (?: [az] + | [AZ] * (? = [AZ] | $))’ , str )

  
# Driver code

str = "GeeksForGeeks"

print (camel_case_split ( str ))