Python 3 Script to Convert CamelCase String to SnakeCase String Using Regex Full Project For Beginners

 

 

app.py

 

 

# Python3 program to convert string
# from camel case to snake case

def change_case(str):
	res = [str[0].lower()]
	for c in str[1:]:
		if c in ('ABCDEFGHIJKLMNOPQRSTUVWXYZ'):
			res.append('_')
			res.append(c.lower())
		else:
			res.append(c)
	
	return ''.join(res)
	
# Driver code
str = "GeeksForGeeks"
print(change_case(str))

 

 

# Python3 program to convert string
# from camel case to snake case
import re

def change_case(str):
	s1 = re.sub('(.)([A-Z][a-z]+)', r'\1_\2', str)
	return re.sub('([a-z0-9])([A-Z])', r'\1_\2', s1).lower()
	
# Driver code
str = "GeeksForGeeks"
print(change_case(str))

Leave a Reply