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 Python program to convert camel case string to snake case S Smitha Dinesh Semwal Follow Improve Article Tags : Python Python Programs Python string-programs Practice Tags : python Similar Reads Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien 3 min read Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes 9 min read Input and Output in Python Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is 8 min read Enumerate() in Python enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam 3 min read Like