What Is Snake Case in Python?

A photo of a black snake with the article title overlayed

While Python may be the name of both a programming language and a snake, snake case is an unrelated concept stemming from the way we name things in programming. In the rest of this article, we’ll take a look at what snake case is, when you would use it, and even why you should use it. Let’s dig in!

Table of Contents

Concept Overview

By far, snake case has to be one of the lowest hanging fruit concepts I’ve ever written about, but we’ll see just how hard of a concept it is to cover.

To start, snake case, which is sometimes written as snake_case as should be obvious shortly, describes lowercase phrases where the spaces between words are replaced by underscores. For example, the phrase “red apple” would be written as red_apple in snake case.

In the context of Python, snake case is used for naming things, like functions and variables. Therefore, snake case is what’s known as a naming convention. In general, there are many naming conventions that vary from language to language and even from team to team. But, I would argue that snake case is fairly universal in the Python community. I usually find it a bit odd when it’s not being followed.

Alternatives to snake_case include camelCase, kebab-case, and flatcase, some of which you may even see used in Python for special cases.

Naturally, I’m sure this short introduction left you will some questions, so let’s get to them!

When to Use Snake Case in Python?

When writing code in Python, you will have plenty of opportunities to name things. For example, whenever you create a variable, you will have to give it a name. The same goes for functions, parameters, modules, classes, and constants. But, not all of them should make use of snake case.

Explicitly, snake case is used when naming variables, functions, parameters, and modules—as in my_variable, my_function, my_parameter, and my_module, respectively. In contrast, upper camel case is used when naming classes, as in MyClass, and upper case is used with constants, as in MY_CONSTANT. Here’s that same information in a table:

Python ContextCasing
Functions, Variables, Parameters, and Modulessnake_case
ClassesUpperCamelCase
ConstantsUPPER_CASE

Generally, I would not expect to see camel case, kebab case, or flat case anywhere in Python code, but I’m sure people do it. Seeing camel case is usually a way of detecting a Java programmer, as I used to be one myself.

Also, I should mention, as rzuckerm has pointed out in the DiscordOpens in a new tab., that several of the Python core libraries disobey this convention, such as logging and threading. Fortunately, the Python developers are aware of this and have carved out an exception for camelCase (or mixedCase as they call it) in PEP8Opens in a new tab..

Why Follow Snake Case in Python?

The question of why you should follow snake case or any rules, really, is more of a philosophical debate than a practical one, but I’ll entertain it.

One possible argument would be one of efficiency. Typically, developers want to eliminate as much cognitive overhead as possible when reading code, and naming conventions are one of our ways of doing that. If you can quickly tell what a name means by glancing at it, you don’t have to bother hunting down its origins.

Another possible argument would be one of polish. As a craft, software development deserves as much care as any other craft. For example, how many of those videos have you seen of rushed construction jobs where tools and junk are left hidden in the walls of a house? Now imagine your users seeing your source code that does not subscribe to any rules.

Perhaps another argument could be around teamwork. Generally, this argument ties into efficiency, but it’s normal to want to use a shared language when you communicate with your team. Believe it or not, code is a form of communication and using the right language (e.g., snake_case in python) shows that you’re a part of the ingroup (i.e., a real software developer).

Lastly, one argument could be around selfishness. When I teach folks how to code, it can be hard to get students to subscribe to conventions, especially if they have previous coding experience. My trick to get folks to care about rules is to center them in the conversation. Forget some hypothetical team. You’re the one who is going to be reading that code again later, so why not do your future self a favor?

What Naming Conventions Do Other Languages Use?

While this is a wonderful question, I cannot even begin to answer it. Not all languages have a strong community around them or have similar philosophical leanings that would cause the community to adopt certain conventions.

That said, there are a few conventions I know off the top of my head. As I said previously, Java is somewhat famous for following the camel case convention for everything but classes, which follow upper camel case, and constants, which follow upper case.

In a similar vein, C# follows a lot of the same conventions as Java, as they are very similar languages. Though, I’m sure there are minor differences, like the way the C# community prefers to place their braces. Likewise, I would suspect that Kotlin also follows the same conventions.

It’s been some time since I’ve used many C-style languages, such as C, C++, and even JavaScript, so I don’t recall the naming conventions in those spaces. That said, I consider those languages and their communities to be a bit of the wild west, so I would imagine there aren’t many universal conventions.

Beyond that, I will say that the Sample Programs repoOpens in a new tab., which contains code snippets in a bunch of programming languages, has a spot to declare the casing of at least the file names for every programming language in the repo. So, you might take a peek there!

Learning About Conventions

Generally, programming languages, like Python, are going to have community-driven conventions. These conventions can be subscribed to in whole or in part depending on the context. As a result, you may find that some of your personal conventions are banned in certain teams, and that’s okay! Every group is going to have their reasons for why they prefer certain conventions over others. These cause arguments every day in our communities, so I don’t expect them to stop anytime soon.

As a matter of fact, I’ve been involved in a few of those debates myself, which you can read about below:

With that said, know that many of the strong opinions you’ll hear from people in tech are just that, opinions. As far as I know, the conventions we hold dear have rarely been researched, if ever, so there’s little to no empirical evidence driving these debates. Though, it looks like there may be some research coming down the pipelineOpens in a new tab., which is exciting!

Also, while you’re hear, why not browse some of the following resources (#ad):

Likewise, you’re welcome to take your support a step further by checking out my list of ways to grow the site. Otherwise, take care, and hopefully I’ll see you again soon!

The Python Concept Map (15 Articles)—Series Navigation

An activity I regularly do with my students is a concept map. Typically, we do it at the start and end of each semester to get an idea of how well our understanding of the material as matured over time. Naturally, I had the idea to extend this concept into its own series, just to see how deeply I can explore my own knowledge of Python. It should be a lot of fun, and I hope you enjoy it!

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife and kid, playing Overwatch 2, Lethal Company, and Baldur's Gate 3, reading manga, watching Penguins hockey, and traveling the world.

Recent Code Posts