Convert camelcase string into underscored one
def camel_to_underscore(string):
"""
Convert a CamelCased string into an underscored one
Usage :
>>> camel_to_underscore("ThisIsATest")
this_is_a_test
"""
list_string = []
for c in string[1:]:
if c.isupper():
list_string.append("_" + c.lower())
else:
list_string.append(c)
return string[0].lower() + ''.join(list_string)
Written by Yohann Gabory
Related protips
1 Response
import re
def cameltounderscore(string):
def repl(match): return '_%s' % match.group(0).lower()
return re.sub('[A-Z]', repl, string)[1:]
</pre>
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Best
#Python
Authors
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#