0

In some languages, it’s common to use camel case (a variable for a user’s first name might be called firstName, and a variable for a user’s preferred first name (e.g., nickname) might be called preferredFirstName.

Python, by contrast, recommends snake case, whereby words are instead separated by underscores (_), with all letters in lowercase. For instance, those same variables would be called name, first_name, and preferred_first_name, respectively, in Python.

In a file called camel.py, implement a program that prompts the user for the name of a variable in camel case and outputs the corresponding name in snake case. Assume that the user’s input will indeed be in camel case.

What I have so far is this

def main():
   for x in camel:
          "_"
camel = input("camelCase: ")
snake = camel
print("snake_case: ", snake )

I know its wrong in probably a few places but im very new at this, doing my best. Using "for" confuses me a lot but in the assignment under "hint" it mentions using the for loop so im not sure how to tie it in. Any help is appreciated thank you.

majk
  • 1
  • 2
  • Just to add i know i dont have anything about having it in lowercase yet i was going to get to that once i figured out the underscore. – majk Sep 17 '22 at 22:42
  • The loop in the beginning does nothing, the rest of the code just prints the output. What is your question. – Yuri Ginsburg Sep 17 '22 at 22:50
  • @YuriGinsburg , in the post i said i need to take input as camel case and have it output in snake case. i also mentioned the assignment mentions using "for" so im supposed to. – majk Sep 17 '22 at 22:53
  • All those are _not_ the questions. See https://stackoverflow.com/help/how-to-ask Also take a look at Python's string functions like `isupper`, `tolower`, `replace`. – Yuri Ginsburg Sep 17 '22 at 23:03
  • Could be a good opportunity for a regex solution. `re.sub(r'(?<=[a-z0-9])([A-Z])', lambda m: f"_{m.group(1).lower()}", "helloWorld1FooBBar")` => `'hello_world1_foo_bBar'` – Chris Sep 17 '22 at 23:04
  • @YuriGinsburg it is the question tho if youd read the post youd see the assignment says to output in snake case, which is what my question is asking how to do. – majk Sep 17 '22 at 23:10
  • @Chris the instruction to use a `for` loop is probably so someone doesn't try to be overly clever with something like regular expressions. – Mark Ransom Sep 17 '22 at 23:49
  • @MarkRansom That's why I didn't feel bad about posting that solution when the OP had shown not a lot of effort. – Chris Sep 17 '22 at 23:53

3 Answers3

1

Get a string in camel case. Then loop over the characters, and whenever you find an uppercase character replace it by an underscore followed by the lower case version of that character.

So all you need is a loop and string operations.

Queeg
  • 7,748
  • 1
  • 16
  • 42
0

This method does what you want:

import re

def camelcase_to_snakecase(camelcase_string):
    uppers = re.findall("[A-Z]", camelcase_string)
    snakecase_string = camelcase_string

    for upper in uppers:
        snakecase_string = snakecase_string.replace(upper, "_"+upper.lower())

    return snakecase_string
HuLu ViCa
  • 5,077
  • 10
  • 43
  • 93
0
import re
snake_case = re.sub('(?<!^)(?=[A-Z])', '_', 'someCamelCase').lower()

(?<!^) negative look behind start of a line (we don't want to have underscore if the first character is upper)

(?=[A-Z]) positive look ahead, matching any upper case.

Finally return copy with lower case.

pepedo
  • 1
  • 1