Program to convert the given string from snake Case to Pascal case using Python

Introduction

In this section of python programming, our task is to convert the case of given string.

Program to convert the given string from snake Case to Pascal case

Program

import string


input_string = 'Welcome_to_gocoding'
print("Input string is: "+input_string + "\n")


#Using capwords()
output1 = string.capwords(input_string.replace("_"," ")).replace(" ", " ")
print("Output using capwords() : "+str(output1) + "\n")


#Using replace() + title()
output2 = input_string.replace("_", " ").title().replace(" ", " ")
print("Output using replace() + title() : "+str(output2))

Output

Program to convert the given string from snake Case to Pascal case Output

Explanation

In the above code, we have used two ways to convert the case of given string: 1. Using capwords() function; 2. Using replace() with title() function.

The capwords() function splits the strings, capitalize word and join the words using join. The output string is printed on the screen by using print function.

The second way we have used is replace() with title() function. Firstly we convert the underscore ( _ ) to empty string and then capitalise the words using title.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.