Is python camelcase or snake case?

Camel case and snake case stand at opposite ends of the variable naming convention spectrum.

When multiple words are used to form a variable, camel case joins those words together, without any white space, and delineates the start of each new word with a capital letter.

In contrast, snake case uses an underscore between words to create separation. Variable names cannot contain spaces, which reduces readability when multiple words must be used to describe a variable's purpose. The underscore in a snake-cased variable tries to minimize this shortcoming.

Snake case examples

The following variable names follow the snake case naming convention:

  • this_is_snake_case
  • build_docker_image
  • run_javascript_function
  • call_python_method
  • ruby_loves_snake_case

Some languages, like Java, use snake case with all uppercase letters to indicate a variable is a constant. For example, INTEREST_RATE would be a Java constant. When all of the snake-cased letters are uppercase, this is known as screaming snake case.

Camel case examples

The following are examples of variables that use the camel case naming convention:

  • FileNotFoundException
  • toString
  • SpringBoot
  • TomcatServer
  • getJson

When the first letter of a camel-cased variable is uppercase, it is also known as Pascal case or upper camel case. When it is not, it is often referred to as lower camel case.

Snake case vs. camel case usage

While individual languages specify their own naming conventions, there is little consistency across languages regarding camel case versus snake case usage.

Languages such as Java and Kotlin, which have a C and C++ heritage, use lower camel case for variables and methods, and upper camel case for reference types such as enums, classes and interfaces. Screaming snake case is used for variables.

Scripting languages, as demonstrated in the Python style guide, recommend snake case in the instances where C-based languages use camel case.

Camel case vs. snake case in JavaScript

When browser-based programming with HTML and JavaScript first emerged, snake case was a common approach to variable and method naming. But as object-oriented derivations of JavaScript, such as TypeScript, AngularJS and Node.JS, gained popularity, camel case has become the standard.

Variable naming conventions, such as snake case or camel case, are standardized differently for each programming language. Furthermore, independent software development teams often break these rules when corner cases are encountered.

When you learn a new software development language, or join a new development team, always be sure to appraise yourself of the variable naming conventions used to ensure that all your co-developers are speaking the same language.

There are many styles.

Some languages, for whatever reason -- usually tracing back to some esoteric preference on the part of the originator -- prefer one style over another.

Learn to adapt to local styles, it will benefit you greatly over your career. Personally, I *hate* CamelCase but I'll use it if the project calls for it.

Local styles

  • Local to language style guides (e.g. PEP8)

  • Local to the team you work with

  • Local to the open source project you contribute to

  • etc

Styles exist for a reason. But I think everyone will agree that the biggest possible style no no is breaking with the local style in use, for whatever value of "local"

For example, I find it extremely annoying when I find code samples -- on SO, say -- for python, and they examples all use CamelCase var names.

Edit: As far as "real world" goes, I've seen people get fired over their inability to adapt to the local style in use by their team. (fwiw, those people are usually also selfish dicks in other ways, as well)

Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

Posted by Alexander Davison

I am starting to like camelCase (with the very first letter lowercased) more then snake_case because it's faster to type. However, Kenneth Love says that it's better to use snake_case for variable names, function names, file names, etc. and CamelCase (with first letter uppercased) for class names.

Should I use camelCase instead of snake_case? Is this conventional or is it up to you to choose?

Any help appreciated.

1 Answer

Is python camelcase or snake case?

STAFF

Straight from the creators of Python, naming conventions. You should only use StudlyCaps for class names. Constants should be IN_ALL_CAPS with underscores separating words. Variable, method, and function names should always be snake_case (wonder where it got that name?). You'll see camelCase in some really old Python code but it's not the modern convention at all.

Write your code according to the conventions so other Python developers will know what kind of construct they're dealing with when reading your code. Your code will also fit in better with open source projects or companies that work you with.

Is Python written in camelCase?

Camel case (ex: camelCase) is usually for variables, properties, attributes, methods, and functions. Snake case (ex: snake_case) is commonly used in scripting languages like Python and as a constant in other C-styled languages. Pascal case (ex: PascalCase) is mainly reserved for class names, interfaces, and namespaces.

Should a Python have a camelCase?

1 Answer. PEP8 is Pythons official style guide and it recommends : Function names should be lowercase, with words separated by underscores as necessary to improve readability.

Are Python variables camelCase or underscore?

Naming Styles.

What is camel casing in Python?

Camel case (sometimes stylized as camelCase or CamelCase; also known as camel caps or more formally as medial capitals) is the practice of writing phrases without spaces or punctuation, indicating the separation of words with a single capitalized letter, and the first word starting with either case.