free stats

Python Variable Naming

Last Update : 20 Aug, 2022 Python, Programming

In this tutorial, You will learn about Python variable naming.

Variable naming is very important and useful in any programming language. Here, you can see various Python variable naming which you should use while Python programming.

A variable name can be a short name such as x, y, z, etc. Also, a variable can be a more descriptive name such as name, age, gender, site_name, etc.

 

To write correct variable names in a Python program, you should follow the following set of rules.

  • Variable names should only contain alpha-numeric characters and underscores such as A-z, 0-9, and _.
  • Variable names should start with the underscore character or a letter.
  • Variable names cannot start with a number.
  • Variable names are case-sensitive which means the variables of name, Name, and NAME are three different variables. 

 

The example of legal variable names is as follows.

myvariable = "UXPython"
my_ariable = "UXPython"
_my_variable = "UXPython"
myVariable = "UXPython"
MYVARIABLE = "UXPython"
my2ariable = "UXPython"

 

The example of Illegal variable names is as follows.

2myvariable = "GUI Programming"
my-variable = "GUI Programming"
my variable = "GUI Programming"

 

Python Multi Words Variable Naming

It is difficult to read the variable names with more than one word. But, sometimes, you have to make variables with more than one word according to the program you have written.

There are several techniques you can use to make multi-word variables with more readability.

These techniques are Pascal Case, Camel Case, and Snake Case.

 

Pascal Case

Pascal Case is a programming practice of writing variables without spaces or punctuation. Also, This is a naming convention of programming.

In the Pascal Case, Capitalizes the first letter of each compound word in a variable. 

Here, Using a single uppercase letter for each additional word makes it easier to discern the purpose of variables and read code.

For example -:

# Pascal Case variable naming examples

TotalValue = 225
ItemNumber = "A234MD"
MasterCard = 313545607432
URLName = "https://www.uxpython.com"

 

Camel Case

Camel Case is also a programming practice of writing variables without spaces or punctuation. Also, This is a naming convention of programming.

In the Camel Case, Capitalizes the first letter of each compound word except the first letter in a variable. 

For example -:

# Camel Case variable naming examples

totalValue = 225
itemNumber = "A234MD"
masterCard = 313545607432
siteName = "https://www.uxpython.com"
indntityCardNumber = "199319213837"

 

Snake Case

Snake Case is also a programming practice of writing variables without spaces or punctuation. Also, This is a naming convention of programming.

In the Snake Case, Each word used in a variable is separated by an underscore character.

For example -:

# Snake Case variable naming examples

total_value = 225
item__umber = "A234MD"
_master_card = 313545607432
site_name = "https://www.uxpython.com"
indntity_card_number = "199319213837"

 

Summary

This tutorial discusses Python variable naming, a Set of rules used for making a correct variable in Python programming, and Multi-word variable naming techniques such as Pascal Case, Camel Case, and Snake Case. Now you can make correct variable names in Python by following this article.

You found this tutorial / article valuable? Need to show your appreciation? Here are some options:

01. Spread the word! Use following buttons to share this tutorial / article link on your favorite social media sites.

02. Follow us on Twitter, GitHub ,and Facebook.