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

Shop

Learn programming in R: courses

$

Best Python online courses for 2022

$

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

$

Latest questions

NUMPYNUMPY

Common xlabel/ylabel for matplotlib subplots

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

12 answers

NUMPYNUMPY

Flake8: Ignore specific warning for entire file

12 answers

NUMPYNUMPY

glob exclude pattern

12 answers

NUMPYNUMPY

How to avoid HTTP error 429 (Too Many Requests) python

12 answers

NUMPYNUMPY

Python CSV error: line contains NULL byte

12 answers

NUMPYNUMPY

csv.Error: iterator should return strings, not bytes

12 answers


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

sin

How to specify multiple return types using type-hints

exp

Printing words vertically in Python

exp

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

cos

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically

# 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 ))