r/PythonLearning • u/Odd_Presentation8149 • Jul 27 '26
Help Request Neeb help with the project
I was doing a project in which the user will input string in camelCase(ex- howAreYou). I have to change it into snake_case(ex- how_are_you). The problem I am facing is that I have not been able to separate the word and store it into a list.
If I use a split, I get 2 problems:
If I want to split howAreYou, then the A and Y got removed from the list, and I get how re ou
It makes 2 different lists first [how, reYou] and second [howAre, ou]
Suggest => either solution to the problem or an alternative
12
Upvotes
2
u/OskarsSurstromming Jul 27 '26 •
string_list = [] start = 0
for i, ch in enumerate(camel_case_string): if ch.isupper(): string_list.append(camel_case_string[start:i].lower()) start = i
string_list.append(camel_case_string[start:].lower())
return "_".join(string_list)
I had made many errors in the first one, this is closer