Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
Convert string to title case in Python
Next article icon

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) 

Output
GeeksforgeeksIsBest 

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 Content

  • Using 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) 

Output
GeeksforgeeksIsBest 

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) 

Output
GeeksforgeeksIsBest 

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) 

Output
GeeksforgeeksIsBest 

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 .


Next Article
Convert string to title case in Python
author
manjeet_04
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. [GFGTABS] Python s = "geeks for geeks" result = s.title() print(result) [/GFGTABS]OutputGeeks For Geeks Explanation: The s.title() method con
    2 min read
  • Convert Decimal to Other Bases in Python
    Given a number in decimal number convert it into binary, octal and hexadecimal number. Here is function to convert decimal to binary, decimal to octal and decimal to hexadecimal. Examples: Input : 55 Output : 55 in Binary : 0b110111 55 in Octal : 0o67 55 in Hexadecimal : 0x37 Input : 282 Output : 28
    2 min read
  • Convert string to a list in Python
    Our task is to Convert string to a list in Python. Whether we need to break a string into characters or words, there are multiple efficient methods to achieve this. In this article, we'll explore these conversion techniques with simple examples. The most common way to convert a string into a list is
    2 min read
  • Convert Decimal to String in Python
    Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing the information about converting decimal to string. Converting Decimal to String str() method can be used to convert decimal to string in Python. Syntax: str(object, encoding=’ut
    1 min read
  • Convert String to Long in Python
    Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing information about converting a string to long. Converting String to long A long is an integer type value that has unlimited length. By converting a string into long we are transl
    1 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
    8 min read
  • How to Convert Bytes to String in Python ?
    We are given data in bytes format and our task is to convert it into a readable string. This is common when dealing with files, network responses, or binary data. For example, if the input is b'hello', the output will be 'hello'. This article covers different ways to convert bytes into strings in Py
    2 min read
  • Convert String to Set in Python
    There are multiple ways of converting a String to a Set in python, here are some of the methods. Using set()The easiest way of converting a string to a set is by using the set() function. Example 1 : [GFGTABS] Python s = "Geeks" print(type(s)) print(s) # Convert String to Set set_s = set(s
    1 min read
  • Convert Object to String in Python
    Python provides built-in type conversion functions to easily transform one data type into another. This article explores the process of converting objects into strings which is a basic aspect of Python programming. Since every element in Python is an object, we can use the built-in str() and repr()
    2 min read
  • Convert String to Int in Python
    In Python, converting a string to an integer is important for performing mathematical operations, processing user input and efficiently handling data. This article will explore different ways to perform this conversion, including error handling and other method to validate input string during conver
    3 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences