Over a million developers have joined DZone.

Converting Between Different Naming Convetions

DZone's Guide to

Converting Between Different Naming Convetions

Free Resource
From Sami Hangaslammi's recipe.

import re

def cw2us(x): # capwords to underscore notation
    return re.sub(r'(?<=[a-z])[A-Z]|(?
  ", x).lower()

def mc2us(x): # mixed case to underscore notation
    return cw2us(x)

def us2mc(x): # underscore to mixed case notation
    return re.sub(r'_([a-z])', lambda m: (m.group(1).upper()), x)

def us2cw(x): # underscore to capwords notation
    s = us2mc(x)
    return s[0].upper()+s[1:]


>>> cw2us("PrintHTML")
>>> cw2us("IOError")
>>> cw2us("SetXYPosition")
>>> cw2us("GetX")

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}