Convert Snake Case String to Camel Case using Python


The snake case string is the naming convention where words are instead separated by an underscore(‘-’). The Camel case is defined by two or more words connected together without spaces and the first letter of each word being capitalized. There are various built-in methods in Python such as split(), join(), and, re.sub() that can be used to convert Snake Case String to Camel Case using Python.

Let’s take an example of this −

  • Snake Case String − hello_world(having separator i.e. underscore ‘_’)

  • Camel Case String − helloWorld(when we convert the Snake case into Camel case it allows the second word of first letter would be capital and without having any space and separator).

Syntax

The following syntax is used in the examples −

split()

The split() is a built-in method in Python that helps to set the new substring from the string. This method divides a string into a list of strings after breaking it with the provided separator.

join()

The join() is a built-in method in Python that can be used to connect all the strings together.

re.sub()

The re.sub(pat, replacement, str) function looks for and substitutes all instances of the pattern in the provided string.

title()

The title method is a built-in library in Python that converts the string of the first letter into upper case.

Using Split() and Join() Method

The program uses a method named split() to split the list of string and also use the method named join() that will be used to connect the two words without having any space.

Example

In the following example, we will first define the function named snake_to_camel_case that converts a snake_case_string to a camelCase. To construct the camelCase version, it divides the string at underscores, capitalizes the subsequent words, and combines them. Finally, we are printing the result as “blackBox”.

def snake_to_camel_case(snake_case_string):
   words = snake_case_string.split('_')
   camel_case_string = words[0].lower() + ''.join(word.title() for word in words[1:])
   return camel_case_string

# Store input for snake case string
snake_case_str = "black_box"
result = snake_to_camel_case(snake_case_str)
print(result)

Output

blackBox

Using Regular Expression and Re.sub()

The program uses regular expressions that will help to convert the Snake Case String into Camel Case String.

Example

In the following example, the function named snake_to_camel_case uses a regular expression to convert a snake_case_string to camelCase. It substitutes the uppercase version of the letter for underscore followed by a letter. Then the final result becomes

“kaliLinux”.

import re

def snake_to_camel_case(snake_case_string):
   camel_case_string = re.sub(r'_([a-zA-Z])', lambda match: match.group(1).upper(), snake_case_string)
   return camel_case_string

# Input of snake case string
snake_case_str = "kali_linux"
result = snake_to_camel_case(snake_case_str)
print(result)

Output

kaliLinux

Using List Comprehension and ''.join()

The program uses the method named join() with an empty string that will be used to store the new string and use list comprehension in for loop to slice the separator i.e. ‘_’. This is the way to get the conversion of Snake Case String into Camel Case String.

Example

In the following example, start with the function snake_to_camel_case that converts a snake_case_string to camelCase. To access individual words, it separates the string at underscores. It capitalizes each word and concatenates it with the previous words beginning with the second. Next, the final result will become “softwareDevelopment”.

def snake_to_camel_case(snake_case_string):
   words = snake_case_string.split('_')
   camel_case_string = ''.join(words[0] + word.title() for word in words[1:])
   return camel_case_string
# Input of snake case string
snake_case_str = "software_development"
result = snake_to_camel_case(snake_case_str)
print(result)

Output

softwareDevelopment

Using a Loop and String Concatenation

The program uses the string concatenation that will create the new formation of the string and loop it will iterate the string and return the result in the transformation of the Camel Case string.

Example

In the following example, the snake_case_string is converted to camelCase using the snake_to_camel_case function. It separates the string at underscores and loads the first word into camel_case_string. Then it uses the for loop through the remaining words, capitalizing each one and appending it to the camel_case_string. Finally, we are printing the result as “pythogorasTheorem”.

def snake_to_camel_case(snake_case_string):
   words = snake_case_string.split('_')
   camel_case_string = words[0]
   for word in words[1:]:
      camel_case_string += word.title()
   return camel_case_string

# Input of snake case string
snake_case_str = "pythogoras_theorem"
result = snake_to_camel_case(snake_case_str)
print(result)

Output

pythogorasTheorem

Conclusion

We discussed the various ways of method to solve this article problem statement. The snake case and camel case string are used to represent the naming convention that used variables name in programming. The conversion of two different strings is the way to build the logic of the program that will be used to represent different style formats.

Updated on: 17-Jul-2023

878 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements