Python program to convert camel case string to snake case Last Updated : 11 Jan, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report 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. Python import re s1 = "camelCaseString" s2 = re.sub(r'([a-z])([A-Z])', r'\1_\2', s1).lower() print(s2) Outputcamel_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 ManipulationA 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. Python 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) Outputcamel_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 to s2.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 Mappingstr.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. Python 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) Outputcamel_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. The join() 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 Charactersreduce() 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. Python from functools import reduce s1 = "camelCaseString" s2 = reduce( lambda acc, char: acc + ('_' + char.lower() if char.isupper() else char), s1 ) print(s2) Outputcamel_case_string Explanation:reduce() function processes each character in s1, 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. Comment More infoAdvertise with us Next Article Ways to Convert List of ASCII Value to String - Python S Smitha Dinesh Semwal Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python - Convert Snake Case String to Camel Case Converting a string from snake case to camel case involves changing the format from words separated by underscores to a single string where the first word is lowercase and subsequent words start with uppercase letters. Using split()This method converts a snake case string into camel case by splittin 3 min read Python Program for Convert characters of a string to opposite case Given a string, convert the characters of the string into the opposite case,i.e. if a character is the lower case then convert it into upper case and vice-versa. Examples: Input : geeksForgEeksOutput : GEEKSfORGeEKSInput: hello every oneOutput: HELLO EVERY ONEExample 1: Python Program for Convert ch 6 min read Python | Split CamelCase string to individual strings Given a string in a camel-case, write a Python program to split each word in the camel case string into individual strings. Examples: Input : "GeeksForGeeks" Output : ['Geeks', 'For', 'Geeks'] Input : "ThisIsInCamelCase" Output : ['This', 'Is', 'In', 'Camel', 'Case'] Method #1: Naive Approach A naiv 4 min read Convert String List to ASCII Values - Python We need to convert each character into its corresponding ASCII value. For example, consider the list ["Hi", "Bye"]. We want to convert it into [[72, 105], [66, 121, 101]], where each character is replaced by its ASCII value. Let's discuss multiple ways to achieve this.Using List Comprehension with o 2 min read Ways to Convert List of ASCII Value to String - Python The task of converting a list of ASCII values to a string in Python involves transforming each integer in the list, which represents an ASCII code, into its corresponding character. For example, with the list a = [71, 101, 101, 107, 115], the goal is to convert each value into a character, resulting 3 min read Convert String to Tuple - Python When we want to break down a string into its individual characters and store each character as an element in a tuple, we can use the tuple() function directly on the string. Strings in Python are iterable, which means that when we pass a string to the tuple() function, it iterates over each characte 2 min read Like