A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/python-convert-snake-case-to-pascal-case/ below:

Python - Convert Snake case to Pascal case

Python - Convert Snake case to Pascal case

Last Updated : 12 Jul, 2025

Converting a string from snake case to Pascal case involves transforming a format where words are separated by underscores into a single string where each word starts with an uppercase letter, including the first word.

Using title()

This method converts a snake case string into pascal case by replacing underscores with spaces and capitalizing the first letter of each word using the title() function. After capitalizing, the words are joined back together without spaces.

Python
s = 'geeksforgeeks_is_best'
res = s.replace("_", " ").title().replace(" ", "")
print(res)

Output
GeeksforgeeksIsBest

Explanation:

Using split()

This method splits the string by underscores, capitalizes each word and then joins them back together. It is efficient in terms of both time and readability.

Python
s= 'geeksforgeeks_is_best'
res = ''.join(word.capitalize() for word in s.split('_'))
print(res)

Output
GeeksforgeeksIsBest

Explanation:

Using re.sub()

Regular expressions are highly flexible and can be used to replace underscores and capitalize the first letter of each word efficiently.

Python
import re

s= "geeksforgeeks_is_best"
res= re.sub(r"(^|_)([a-z])", lambda match: match.group(2).upper(), s)
print(res)

Output
GeeksforgeeksIsBest

Explanation:

Using for loop

This approach manually iterates through each character in the string, capitalizes the first letter after an underscore and constructs the PascalCase string incrementally.

Python
s= 'geeksforgeeks_is_best'
res = ""
capNext = True  # Flag to track if the next character should be capitalized

for char in s:
    if char == '_':  
        capNext = True 
    elif capNext:  
        res += char.upper()  # Capitalize the current character
        capNext = False 
    else:
        res += char  # Add the character as it is

print(res)

Output
GeeksforgeeksIsBest

Explanation:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4