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.
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.
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"
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'
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)])