Python - Convert Snake case to Pascal case Last Updated : 16 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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) OutputGeeksforgeeksIsBest Explanation:replace("_", " "): This handles the conversion of underscores to spaces.title():This ensures that the first letter of each word is capitalized.replace(" ", ""): This removes any spaces, ensuring the result is in pascal case.Table of ContentUsing split()Using re.sub()Using for loop 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) OutputGeeksforgeeksIsBest Explanation:split('_') splits the string s into individual words at each underscore.capitalize() capitalizes the first letter of each word.''.join() joins the list of words into a single string without any separator.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) OutputGeeksforgeeksIsBest Explanation:(^|_) matches either the start of the string (^) or an underscore (_).([a-z]) matches any lowercase letter (a-z) following the start of the string s or an underscore.lambda match: match.group(2).upper() converts the matched lowercase letter (group 2) to uppercase.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) OutputGeeksforgeeksIsBest Explanation:for char in s iterates through each character in the string s.if char == '_' sets capNext = True to indicate the next character should be capitalized.elif capNext: If the flag is True, the character is converted to uppercase and appended to res and the flag is reset with capNext = False.else: If the flag is False, the character is appended to res . Comment More infoAdvertise with us Next Article Python - Convert Snake case to Pascal case M manjeet_04 Follow Improve Article Tags : Python Python string-programs Practice Tags : python Similar Reads Convert string to title case in Python In this article, we will see how to convert the string to a title case in Python. The str.title() method capitalizes the first letter of every word.Pythons = "geeks for geeks" result = s.title() print(result) OutputGeeks For Geeks Explanation:The s.title() method converts the string "python is fun" 2 min read Python Match Case Statement Introduced in Python 3.10, the match case statement offers a powerful mechanism for pattern matching in Python. It allows us to perform more expressive and readable conditional checks. Unlike traditional if-elif-else chains, which can become unwieldy with complex conditions, the match-case statement 7 min read Toggle characters in words having same case - Python We are given a sentence and need to toggle the case of words that have all characters in the same case, either all lowercase or all uppercase. If a word meets this condition, we change each letter to its opposite case using swapcase(). Words with a mix of uppercase and lowercase letters remain uncha 3 min read Convert a List of Characters into a String - Python Our task is to convert a list of characters into a single string. For example, if the input is ['H', 'e', 'l', 'l', 'o'], the output should be "Hello".Using join() We can convert a list of characters into a string using join() method, this method concatenates the list elements (which should be strin 2 min read Python string - ascii_lowercase In Python, ascii_lowercase is a useful tool that gives us a string containing all the lowercase letters from 'a' to 'z'. We can use this for tasks like generating random strings or checking if a letter is lowercase. Example:Pythonfrom string import ascii_lowercase res = ascii_lowercase print(res)Out 2 min read Like