Camel Case is most widely used in various programming languages as a naming protocol. Camel Case protocol finds its usage in programming languages that do not allow spaces in the names of variables or identifiers.
CamelCase, also known as camel caps or medial capitals, follows a technique of writing phrases or words without spaces between them.
In this article, we are going to see what a Camel Case is, how to create a camel case, and how to check if a sentence is in a camel case with the help of regular expressions.
What Is Camel Case?
To put it in simple terms, if every starting letter of a word is in upper case, we call it a camel case. However, we have two variants of this.
Pascal Case(Upper Camel Case) – Every first letter of the words is an upper case letter, including the initial phrase.
Examples include – HelloWorldThisIsJava, ThisIsAskPython.
Lower Camel Case- In lower camel case, the initial phrase should consist of a lowercase letter. Microsoft generally uses the term camel case for the lower camel case.
A few examples are helloWorldThisIsJava,thisIsAskPython
Camel Case is most widely used to give descriptive and distinctive names to variables.
CamelCase and snake_case Are Not the Same!
While they may look alike, camel cases and snake cases are not the same.
In the snake case, we use an underscore(
_) to separate the words. And in Camel Case, as we already know, we don’t use spaces.
A few examples of snake cases are this_is_a_snake_case, ask_python.
If you are interested in what a combination of both looks like, follow the example below.
def Calculate_Sum_Of_Numbers(a,b): sum = a+b return sum Calculate_Sum_Of_Numbers(5,10)
In the above code, the definition of the function uses both techniques.
How to Create a Camel Case?
The naive approach is to take each word and make the first letter upper case and the other letter of each word lower case. We follow the same approach below.
We create an empty string and join the result of the camel case operation to it.
def camelcase(list_words): cvt = "".join(word.upper() + word[1:].lower() for word in list_words) return cvt.lower() + cvt[1:] def main(): userInp = input("Enter a sentence or a list of words: ") words = userInp.split() camelCaseResult = camelcase(words) print("Camel case: ", camelCaseResult) if __name__ == "__main__": main()
We are creating a function called camelcase to convert a sentence into camelcase. It takes a list of words from the user. The variable
cvt stores the result of uppercasing the first letter of a word and joins it to an empty string. The input from the user is stored in userInp and is split into individual words, which are then passed to the function, and finally, we get the string in a camel case,
Now that we have understood what a camel case is and how to convert a sentence to camel case, let us now use regular expressions to check if a sentence is in camel case.
How to Check for Camel Case Using Regex?
We can use regular expressions to check if a given sentence is in camel case or not by matching a pattern.
re.match() is used to match a particular pattern with the given string. Let us see the code.
import re def isCamelCase(s): pattern = r'^[a-zA-Z]+([A-Z][a-z]+)+$' return bool(re.match(pattern, s)) userInp = input("Enter a string") print(isCamelCase(userInp))
We are importing the regular expressions(regex) module in the first line.
Next, we create a function called
isCamelCase that takes a string and performs pattern matching.
The pattern for checking if a string is in camel case is
^[a-zA-Z]+([A-Z][a-z]+)+$. It starts from the starting letter. The starting letter can be upper case or lower case. However, the letters after it should be lowercase.
We are using a boolean flag to print if the input by the user is a camel case or not. The match is used to check the pattern.
The input by the user is stored in a variable called userInp and in the last line, we are calling the function(isCamelCase) to check on the user’s input.
Here is the output for different test cases.
To summarize, we have understood what a camel case protocol is and looked at a few examples. To recapitulate, a camel case is a naming protocol that does not allow any spaces, and the initial letters of the word must be in upper case. For example – thisIsTheSummary.
We have seen the difference between the snake case and the camel case with the help of examples and also understood what the combination of both looks like with the help of a program.
Then with the help of a naive approach, we understood how to convert user input to a camel case.
Lastly, we used regular expression pattern matching to check if a string or a sentence is in the camel case or not.