Case Styles in Python
How to write them and its uses according to PEP 8
Like I wrote in the image above, you can write a single string in Python in different ways and each of them has its uses. How do you choose between them? Let’s learning that.
Firstly: PEP 8
According to the official Python documentation, PEP 8 is a document wrote by Guido van Rossum himself that:
gives coding conventions for the Python code comprising the standard library in the main Python distribution.
In other words, PEP 8 is just style conventions for writing Python. You don’t need to use these guidelines, your code will still work. However, according to The Zen of Python, readability counts. So, if you and the whole Python community follow the same guidelines, this will improve the readability of code and create an easier experience of reading others' code.
PEP 8 Naming Conventions
Even if you do not apply all the guidelines from PEP 8, just the naming conventions can play a significant role in how good or bad poorly your code is written. Besides, I think this is the easiest improvement you can get from the guidelines. After reading PEP 8, I selected the most common cases which it is necessary to choose one of the case styles:
- For variables, functions, methods, and modules: Snake Case.
- For classes: Pascal Case.
- For constants: Capitalized Snake Case.
The best way will be that one that improves your code readability. Some languages have naming conventions, like above, that you can use or you can go rogue and write as you like (nobody will criticize you, except your colleagues, they will hate you).
Anyway, let me (finally) explain the cases for combining words and for the examples I will use this:
Base: first name
Camel Case
This case combines words by capitalizing the first letter of each word (except the first one) and removing the space between them:
Get Ayrton Lima’s stories in your inbox
Join Medium for free to get updates from this writer.
Camel Case: firstName
This case is very used in many languages for a variety of purposes and in Python is not much used because Pascal Case is the default approach.
Pascal Case
This case is also called Upper Camel Case, because combines words by capitalizing all words (even the first word) and removing the space between them:
Pascal Case: FirstName
This type is very used in Python for class names: OverflowError or ValueError (Exceptions are classes).
Snake Case
This one is the most used in Python. It combines words by replacing spaces between words with an underscore:
Snake Case: first_name
Sometimes, it is better to use this type all capitalized, also known as Constant Case (used in constants in Python, of course):
Constant Case (Capitalized Snake Case): FIRST_NAME
Kebab Case
This one is like Snake Case but instead of underscore, the dash is used to replace the spaces between words:
Kebab Case: first-name
This case is rare in Python, but can be found a lot in URLs for example. Sometimes is common to use this style when writing namespacing url names in Django (Python Framework).
Conclusion
Like I said, you are free to choose between them, but from my experience, try to use the same as your team. This will help a lot when debugging or just reading someone’s code. Happy coding.

