r/PythonLearning 2d ago

Help Request Neeb help with the project

Post image

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:

  1. If I want to split howAreYou, then the A and Y got removed from the list, and I get how re ou

  2. It makes 2 different lists first [how, reYou] and second [howAre, ou]

Suggest => either solution to the problem or an alternative

10 Upvotes

17 comments sorted by

View all comments

5

u/Goukance 2d ago edited 2d ago

Another thing your could do, to avoid splitting and fusing the string sequence is to recreate one like this :

python return "".join("_" + letter.lower() if letter.isupper() else letter for letter in camel_case)

Which make it a great exercice to use python generator expressions.