Convert Snake case to Pascal case in Python
In this tutorial, we will get to know about the Snake case, Pascal case and also learn about how to convert Snake case to Pascal case in Python programming language.
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 “codes_speedy” and “Codes_speedy”. It is used in functions name, variables name, and some class name in computer software codes.
Pascal Case: In the pascal case, the first letter of each word in a phrase or a compound is always a capital letter.
Like in “CodeSpeedy” and “HelloWorld”. Computer software source code’s name of classes, functions, or other objects are usually in Pascal case.
Python: convert Snake case to Pascal case
When we working with python strings, sometimes we have faced a little problem in which we need to change the case of a string. So let’s discuss a few ways of case conversion of string.
INPUT: code_speedy OUTPUT: CodeSpeedy INPUT: Hello_world OUTPUT: HelloWorld
- Using capwords() function.
- Using title() and replace() functions.
Implementation of Snake case to Pascal case using capwords() in this method.
from string import capwords string = 'code_speedy' print('In Snake Case: ',string) result = capwords(string.replace('_',' ')) result = re.replace(' ','') print('In Pascal Case: ',result)
In Snake Case: code_speedy In Pascal Case: CodeSpeedy
Implement the case conversion using title() and replace() function.
string = 'code_speedy_hello_world' print('In Snake Case: ',string) result = string.replace('_',' ').title() result = result.replace(' ','') print('In Pascal Case: ',result)
In Snake Case: code_speedy_hello_world In Pascal Case: CodeSpeedyHelloWorld
Thanks for visiting CodeSpeedy. I hope it helps you.
Recommended to read: