Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

Write down a python function to convert camel case to snake case?

I have two strings in camel case as follows

s1 = JavaBeans
s2 = JavaStrutsSwing

How do I use a python function to convert the camel case of these strings into snake case?


1 Answer
Rajendra Dharmkar

Code for converting camel case to snake case is given below

import re
def convert(name):
      s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', name)
      return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
print convert('JavaBeans')
print convert('JavaStrutsSwing')

OUTPUT

java_beans
java_struts_swing
Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.