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
# 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 )) |
Exit:
[’Geeks’,’ For’, ’Geeks’]
Shop
Best Python online courses for 2022
$FREE
Best laptop for Excel
$
Best laptop for Solidworks
$399+
Best laptop for Roblox
$399+
Best computer for crypto mining
$499+
Best laptop for Sims 4
$
Best laptop for Zoom
$499
Best laptop for Minecraft
$590
Latest questions
PythonStackOverflow
Common xlabel/ylabel for matplotlib subplots
1947 answers
PythonStackOverflow
Check if one list is a subset of another in Python
1173 answers
PythonStackOverflow
How to specify multiple return types using type-hints
1002 answers
PythonStackOverflow
Printing words vertically in Python
909 answers
PythonStackOverflow
Python Extract words from a given string
798 answers
PythonStackOverflow
Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?
606 answers
PythonStackOverflow
Python os.path.join () method
384 answers
PythonStackOverflow
Flake8: Ignore specific warning for entire file
360 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
How to specify multiple return types using type-hints
Printing words vertically in Python
Python Extract words from a given string
Cyclic redundancy check in Python
Finding mean, median, mode in Python without libraries
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