Answer to Question #167917 in Python for srikanth

Question #167917
Given a string in camel case, write a python program to convert the given string from camel case to snake case.
Input

The input will be a single line contain a string.
Output

The output should contain the given word in snake case.
Explanation

For example, if the given word is "PythonLearning" in camel case, your code should print given word in snake case "python_learning".
Sample Input 1
PythonLearning
Sample Output 1
python_learning

Sample Input 2
CamelCase
Sample Output 2
camel_case
1
Expert's answer
2021-03-02T09:49:54-0500
def convert(word):
    first = True
    for ch in word:
        if first:
            res = ch.lower()
        elif ch.isupper():
            res += '_' + ch.lower()
        else:
            res += ch
        first = False
    return res


if __name__ == '__main__':
    word = input().strip()
    snake = convert(word)
    print(snake)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS