0

Take a string from the user which is written in camel case and then convert it into snake case.

I tried using isupper method to find the uppercase letters in the string but unable to separate them.

3 Answers3

0

Try this:

def camel_to_snake(camel_string):
return ''.join(['_' + ch.lower() if i > 0 and ch.isupper() else ch.lower() for i, ch in enumerate(camel_string)])

Example :

snake_string = camel_to_snake("C4melCa5eString")
print(snake_string)  # Output: "c4mel_ca5e_string"
Parsa Hz
  • 61
  • 4
0
s=input()
''.join(['_'+c.lower() if c.isupper() else c for c in s]).lstrip('_')

Or you can install inflection library

pip install inflection

then:

import inflection
inflection.underscore('MyNameIsTalha')
#'my_name_is_talha'
Talha Tayyab
  • 8,111
  • 25
  • 27
  • 44
0

def camel_to_snake(camel_string): return ''.join(['_' + ch.lower() if i > 0 and ch.isupper() else ch.lower() for i, ch in enumerate(camel_string)])