Python Convert Snake case to Pascal case
In this tutorial, you will learn how to convert snake case to pascal case. Before moving forward let us discuss what are snake and pascal cases.
Snake case: Snake case contains compound words or phrases in which words are separated using one underscore (“_”) and no spaces, with each word’s initial letter usually in lowercase within the phrase or compound. Like in “study_tonight” and “Study_tonight”.
Pascal case: In the pascal case, the first letter of each word in a phrase or a compound is always a capital letter. Without any space between the words. Like in “StudyTonight” and “HelloWorld”. For example,
Input: python_language
Output: PythonLanguage
Input: Hello_world
Output: HelloWorld
Approach
To solve this problem, there are several different approaches, some of them are-
- Using capwords()
- Title() and replace()
Approach 1: capwords()
In this approach, we will use the capwords() function from the string class. This function is used to capitalize all the words in the string. To capitalize the first letter of each word we have used this function. Also, we need to remove any underscore (“_”) or spaces which can be done by using the replace function.
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Import capwords in the program
Step 2- Define string with value in snake case
Step 3- Define a variable to store the converted string
Step 4- Use capwords and replace to capitalise the first letter of each word
Step 5- Replace spaces in the string
Step 6- Print the resultant string
Python Program 1
Look at the program to understand the implementation. To use capwords() we have to import it from the string class in our program.
from string import capwords
string= "program_tutorial"
print("Snake case: ",string)
pascal= capwords(string.replace('_',' '))
pascal= pascal.replace(' ','')
print("Pascal case: ",pascal)
Snake case: program_tutorial
Pascal case: ProgramTutorial
Approach 2: title() and replace()
In this approach, we will use the title() function of the String class which returns the string after capitalizing the first character of every word in the string. To remove underscore and spaces from the string we will use another string function replace().
Algorithm
Follow the algorithm to understand the approach better.
Step 1- Define string with value in snake case
Step 2- Define a variable to store the converted string
Step 3- First replace the underscore in the string with space
Step 4- Call title() to capitalize the first letters of the words
Step 5- Remove spaces from the string
Step 6- Print the resultant string
Python Program 2
Look at the program to understand the implementation. To use capwords() method, we have to import it from the string class in our program.
string= "program_tutorial"
print("Snake case: ",string)
pascal= string.replace('_',' ').title()
pascal= pascal.replace(' ','')
print("Pascal case: ",pascal)
Snake case: program_tutorial
Pascal case: ProgramTutorial
Conclusion
In this tutorial, we have seen what are snake cases and pascal cases. To convert snake case to pascal case we have seen two approaches that we can use in our program.