Python @ DjangoSpin

PyPro #100 Convert camelCase words to underscore_separated_lowercase words

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon
Reading Time: 1 minutes

camelCase to underscore_separated_lowercase in Python

camelCase to underscore_separated_lowercase in Python

Write a Regular Expression in Python to convert camelCase to underscore_separated_lowercase words.

import re

sourceString = 'randomCamelCaseVariableOne randomCamelCaseVariableTwo'

print( re.sub('([a-z]+)([A-Z])', r'\1_\2', sourceString).lower() )	# random_camel_case_variable_one random_camel_case_variable_two

To learn more about Regular Expressions in Python, click here.


See also:

Buffer this pageShare on FacebookPrint this pageTweet about this on TwitterShare on Google+Share on LinkedInShare on StumbleUpon

Leave a Reply