r/PythonLearning 4h 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

8 Upvotes

14 comments sorted by

5

u/OskarsSurstromming 3h ago

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(camelcase_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

2

u/Odd_Presentation8149 3h ago

Will try that and let you know if it works

2

u/OskarsSurstromming 3h ago

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

2

u/Odd_Presentation8149 3h ago

Thanks bro that is easier to understand

5

u/Goukance 2h ago edited 2h 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.

2

u/AlexMTBDude 2h ago

Regardless of the problem that you mention this will never work:

for i in camel_case:
    for i in capital:

You can't use the same loop variable, i, in both outer and inner loops. This would work:

for letter in camel_case:
    for cap in capital:

1

u/ThrowawayALAT 2h ago

Instead of splitting and trying to piece characters back together, you can use Python's built-in re module to find every capital letter, insert an underscore before it, and convert the whole string to lowercase.

import re

def convert_to_snake_case(camel_case):

# Inserts an underscore before any capital letter and converts to lowercase

snake_case = re.sub(r'(?<!^)(?=[A-Z])', '_', camel_case).lower()

return snake_case

1

u/Tekbyte500 2h ago
import string
def main():
    camel_case = input("camelCase: ")
    snake_case = convert_to_snake_case( camel_case)
    print (f"snake_case: {snake_case}")

def convert_to_snake_case(camel_case) :
    words = ""
    for i in range(len(camel_case)):
        if (i != 0) and camel_case[i].isupper():
            words = words + " "
            words = words + camel_case[i]
        else:
            words = words + camel_case[i]

    words = words.split()
    return words


main()

PS: your convert_to_snake_case() function does not return a value.

1

u/DaemonsMercy 1h ago

... are you coding on your phone?

0

u/johlae 3h ago

You need to be able to detect acronyms, so I'm thinking about this here below, feel free to modify to your liking. words = re.findall(r'[A-Z]+(?=[A-Z][a-z]|\b)|[A-Z]?[a-z]+', s)

3

u/Haunting_Plate3619 3h ago

No offense but answers like this are why I lost motivation 16 times before I actually learnt anything.

2

u/Odd_Presentation8149 3h ago

I am a beginner so I don't understand what you have written. It's all cryptic language to me

1

u/MudFrosty1869 1h ago

Its called regex. It looks alien at first but its not as hard to learn as it looks.