Recently in a Python webinar, someone asked me this doubt on how to check if a string is in CamelCase format. After researching and testing different methods, I found several effective solutions. I will help you learn how to check if a string is in CamelCase format using Python in this tutorial by providing suitable examples and screenshots of executed example code.
CamelCase Format
Before we get into the code, let’s clarify what the camelCase format entails:
- The first word in the string starts with a lowercase letter.
- Each subsequent word starts with an uppercase letter.
- There are no spaces or underscores separating the words.
Some examples of strings in camelCase format include:
- “markJohnson”
- “sanFrancisco”
- “myFavoriteMovie”
Read How to Unpack a Tuple in Python?
Check if a String is in CamelCase Format Using Python
Python provides several ways to check for CamelCase. Let us see some important methods.
1. Use Regular Expressions
One elegant way to check if a string is in camelCase format is by using regular expressions (regex). Python provides the re module for working with regular expressions. Let’s see how we can leverage regex to solve our problem.
Step 1: Import the re module
First, we need to import the re module in our Python script:
import reStep 2: Define the regex pattern
Next, we’ll define a regex pattern that matches the camelCase format. The pattern should ensure that:
- The string starts with a lowercase letter.
- Each subsequent word starts with an uppercase letter.
- The string contains only alphanumeric characters.
Here’s the regex pattern we can use:
pattern = r'^[a-z]+(?:[A-Z][a-z]+)*$'Let’s break down the pattern:
^: Matches the start of the string.[a-z]+: Matches one or more lowercase letters.(?:[A-Z][a-z]+)*: Matches zero or more occurrences of an uppercase letter followed by one or more lowercase letters.$: Matches the end of the string.
Step 3: Use the re.match() function
To check if a string matches the camelCase pattern, we can use the re.match() function. This function attempts to match the pattern at the beginning of the string and returns a match object if successful, or None otherwise.
Here’s an example of how to use re.match():
def is_camel_case(string):
return re.match(r'^[a-z]+(?:[A-Z][a-z]+)*$', string) is not None
# Example usage
print(is_camel_case("johnDoe"))
print(is_camel_case("JohnDoe"))
print(is_camel_case("john_doe")) Output:
True
False
FalseI have executed the above example code and added the screenshot below.

In the above code, we define a function called is_camel_case() that takes a string as input. Inside the function, we use re.match() to check if the string matches the camelCase pattern. If a match is found, re.match() returns a match object, which evaluates to True in a boolean context. Otherwise, it returns None , which evaluates to False.
Check out How to Convert an Array to a Tuple in Python?
Example
Let’s consider a real-world scenario where checking for camelCase format can be useful. Suppose you’re building a web application that allows users to create accounts. You want to enforce a naming convention for usernames where they must be in camelCase format.
Here’s how you can validate the username input:
import re
def is_valid_username(username):
return re.match(r'^[a-z]+(?:[A-Z][a-z]+)*$', username) is not None
# Example usage
username = input("Enter your username: ")
if is_valid_username(username):
print("Username is valid!")
else:
print("Username is invalid. Please use camelCase format.")Output:
Enter your username:I have executed the above example code and added the screenshot below.

In this example, we prompt the user to enter their desired username. We then pass the username to the is_valid_username() function, which checks if it follows the camelCase format using the regex pattern. If the username is valid, we display a success message. Otherwise, we inform the user that the username is invalid and guide the expected format.
Read How to Iterate Through Tuples in Python?
Additional Considerations
While checking for camelCase format using regex is a straightforward approach, there are a few additional considerations to keep in mind:
- The regex pattern we used assumes that the string should only contain alphanumeric characters. If you want to allow other characters, such as underscores or hyphens, you’ll need to modify the pattern accordingly.
- If you want to allow the first word to start with an uppercase letter (known as PascalCase or UpperCamelCase), you can modify the regex pattern to
r'^(?:[A-Z][a-z]+)+$'. - Keep in mind that the regex approach only checks if the string follows the camelCase format. It doesn’t validate the meaningfulness or appropriateness of the string itself. Additional validation may be necessary depending on your specific requirements.
Check out How to Append Elements to a Tuple in Python?
Conclusion
In this tutorial, we explored how to check if a string is in CamelCase format using Python. We learned about the characteristics of camelCase and how to use regular expressions to match the desired pattern. By utilizing the re module and the re.match() function, we can easily validate whether a string follows the camelCase convention.
You may also like to read:
- How to Return a Tuple in Python?
- How to Sort by the Second Element in a Tuple in Python?
- How to Sort a Tuple in Python?

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.