Camel case is a writing style in which multiple words are concatenated into a single string, Snake case is another writing style where words are separated by underscores (_) and all letters are lowercase. We are going to cover how to convert camel case into snake case.
Using Regular Expressions (re.sub)
re.sub() function is used to apply a regular expression (([a-z])([A-Z])) that finds lowercase letters followed by uppercase letters.
import re
s1 = "camelCaseString"
s2 = re.sub(r'([a-z])([A-Z])', r'\1_\2', s1).lower()
print(s2)
Output
camel_case_string
Explanation:
re.sub()function inserts an underscore between lowercase and uppercase letters using the pattern([a-z])([A-Z])..lower()method converts the result to lowercase, yielding the snake case format"camel_case_string".
Using a For Loop and String Manipulation
A for loop iterates through each character in the string, checking if it is uppercase. If it is, an underscore is added before converting it to lowercase. The final result is a snake case string.
s1 = "camelCaseString"
s2 = ""
for char in s1:
if char.isupper():
s2 += "_" + char.lower()
else:
s2 += char
# Remove leading underscore if present
if s2.startswith("_"):
s2 = s2[1:]
print(s2)
Output
camel_case_string
Explanation:
- for loop iterates through each character of the string
s, adding an underscore before uppercase characters and converting them to lowercase, while appending other characters directly tos2. - After the loop, the code checks if the string starts with an underscore (which occurs if the first character is uppercase) and removes it if necessary.
Using str.translate() with a Custom Mapping
str.translate() method is used to convert all uppercase letters to lowercase by applying a custom translation table created with str.maketrans(). Then, a list comprehension adds underscores before each uppercase letter to convert the string to snake case.
s1 = "camelCaseString"
s2 = ''.join(['_' + char.lower() if char.isupper() else char for char in s])
# Remove leading underscore if present
if s2.startswith("_"):
s2 = s2[1:]
print(s2)
Output
camel_case_string
Explanation:
- list comprehension iterates through each character in the string
s, adding an underscore and converting uppercase letters to lowercase, while leaving lowercase letters unchanged. Thejoin()method combines the results into a single string. - Afterward, the code checks if the string starts with an underscore and removes it if necessary, ensuring the string is correctly formatted in snake case.
Using reduce() and Accumulating Characters
reduce() function iterates through each character in the string, accumulating the result by adding an underscore before uppercase letters and converting them to lowercase. Final string is built up character by character in snake case format.
from functools import reduce
s1 = "camelCaseString"
s2 = reduce(
lambda acc, char: acc + ('_' + char.lower() if char.isupper() else char),
s1
)
print(s2)
Output
camel_case_string
Explanation:
reduce()function processes each character ins1, accumulating the result by adding an underscore and converting uppercase characters to lowercase.- lambda function is applied to each character, progressively building the string in snake case format, which is then printed.