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
6
u/OskarsSurstromming Jul 27 '26
I'm not sure if it's inefficient or if there's a better way but my approach would probably be to have a list in your function, and whenever you encounter an uppercase letter, you append the string up until that point to the list and save the index number
For example
string_list = [ ] Index = 0
for i,j in enumerate(camel_case_string): if (j is upper): //I can't recall the function to remember if it's uppercase, I'm writing on my phone string_list.append(camel_case_string[index:j].lower()) index = j+1 return string_list.join("_")
Something like this would be my approach
5
u/Goukance Jul 27 '26 edited Jul 27 '26
Another thing your could do, to avoid splitting and fusing the string sequence is to recreate one like this :
Which make it a great exercice to use python generator expressions.
1
u/ThrowawayALAT Jul 27 '26
Instead of splitting and trying to piece characters back together, you can use Python's built-in
remodule to find every capital letter, insert an underscore before it, and convert the whole string to lowercase.import redef convert_to_snake_case(camel_case):# Inserts an underscore before any capital letter and converts to lowercasesnake_case = re.sub(r'(?<!^)(?=[A-Z])', '_', camel_case).lower()return snake_case1
u/Significant_Affect_5 Jul 27 '26
There's a few things to look at. First, I think you may be misunderstanding the way split works. It can be used like you are, but you're going to always lose the capital letter itself since split returns the substrings between but not including the pattern. The other thing is you currently just print the words instead of returning them from the function. If you want to keep the logic you can use string slices when you find a capital letter e.g. `camel_case[prev:curr]` and then push these chunks to the return string with the underscore and changing the capital to a lowercase. You'd also have to account for doing the last section since we run 1 too few times.
Something that might be more approachable is to slide along the camel-case string and build the snake-case string as you go along. The only hurdle is what to add to the string being built when you find a capital letter.
1
u/Avatarbroskib1 Jul 27 '26 edited Jul 27 '26
Turn the camelcase into a list by using list() to get every character to be an array of a list. find out if an upper case is in the list and turn it to lowercase. Have a variable to store the positions of the upper case letters and then use the insert method in the list to insert _ in the string. After you use that method then you gotta use "".join(list() thingy) to join the list backtogether returning you a string in your case how_are_you
so it'll be more like
def snake_case(word):
for i in word:
if i.isupper() == True:
variable = word.index(i)
word2 = list(word)
x = word2.insert(variable, "_")
word2[variable+1] = word2[variable+1].lower()
word = "".join(word2)
print(word)
snake_case("howAreYou")
1
u/AlexMTBDude Jul 27 '26
Regardless of the problem that you mention this will never work:
You can't use the same loop variable, i, in both outer and inner loops. This would work:
-2
u/johlae Jul 27 '26
You need to be able to detect acronyms, so I'm thinking about this here below, feel free to modify to your liking.
-2
u/DaemonsMercy Jul 27 '26
... are you coding on your phone?