def to_camel(name):
first, *rest = name.split("_")
return first + "".join(w.capitalize() for w in rest)
def to_snake(name):
return "".join("_" + ch.lower() if ch.isupper() else ch for ch in name)
for n in ["total_marks", "is_logged_in", "user"]:
camel = to_camel(n)
print(n, "->", camel, "->", to_snake(camel))
Output Verified by running it on Python 3
total_marks -> totalMarks -> total_marks
is_logged_in -> isLoggedIn -> is_logged_in
user -> user -> user
Starred unpacking separates the first word, which stays lower-case. Going back, every capital becomes an underscore plus its lower-case letter.
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!
We use Google Analytics cookies to see how LearnFrenzy is used and make it better, and Google AdSense to keep it free.
No analytics or ad cookies are set until you accept. Privacy Policy
·
Discussion & Comments 0
Share your method, a shortcut, or ask about a step you didn't follow.
No comments yet. Be the first to share your solution!