Python Underscore _ Variable

Published on January 20, 2024

Python includes an underscore ( _ ) variable, which is used to ignore the value in it. If a value is returned from a function and does not need to be used, it can be assigned to _, which is treated like an "ignore variable".

Python underscore _ variable

The underscore has many meanings in Python. In this article, we will show ou where all underscore is used.

Underscore variable _ stores the result of the last expression

Start the Python shell. In the Python shell, perform a math operation 2 + 3.

Then print the value of _. It will print the last evaluated value, which is 5.

>>> 2 + 3
>>> _

Underscore variable _ is used as an ignore variable

For variable assignment while unpacking, if you want to just capture, but ignore a value, underscore variables are used.

Underscore as ignore variable for single value

In this example, we have a tuple (3, 6, 9) and want to assign the first and third items to x and y respectively and ignore the second item:

x, _, y = (3, 6, 9)
print(x, y)


3 9

The second value 6 got assigned into _ as it is the second place.

Underscore as ignore variable to accept a list

In this example, the ignore variable _ accepts a list:

x, *_, y = list(range(10))
print(x, y)


0 9
[1, 2, 3, 4, 5, 6, 7, 8]

This is called Extended Iterable Unpacking (PEP 3132). Here, the middle variable is a catch-all variable that will be assigned a list of all items not assigned to a specific variable name.

This is used in cases where you may need the end values

Underscore as ignore variable to accept value[s] returned from a function

In this example, we call a function that returns multiple values. The ignore variable is positionally placed where one or more of the returned values can bed.

We will call function get_coins() which returns 4 values, and accept values #1, 2 and 4, and ignore the value #3. So, we will place _ in the third position.

def get_coins():
    return 5, 7, 9, 3

x, y, _, z = get_coins()
print(x, y, z)


5 7 3

The third returned value 9 got accepted by _, so it was ignored.

Underscore as ignore variable in loops

The underscore variable can be used as a variable in loops.

This is applicable if we do not need the counter in future.

Example 1: Let us print the first 10 numbers using the for loop.

for _ in range(10):
    print(_ + 1)

This prints the first 10 numbers, from 1 through 10.

Example 2: Let us print the first 10 numbers using the while loop.

_ = 1
while _ <= 10:
    _ += 1

Where else is underscore used?

Underscores are not just used as ignore variable, but they can be used for a number of other things as well. In this case, underscore is a part of something else, a literal or variable name.

A few of the cases are to separate digits for better understandability, including in variable names, either in between, as prefix or suffix. Let us look at them one by one.

Using underscores in numbers to separate digits for better understanding

If you want to separate digits in an numeric variable, you can insert as many underscores as you want in between.

Example 1: Initialize variable i containing a value of 10 million.

i = 10_000_000



Example 2: Initialize binary number b containing a value of 10000000000, which is 1024, or 210 in binary). We will separate it 4 bits at a time, beginning from the rightmost bit.

b = 0b_100_0000_0000



Using underscores in variables for better understanding

You are probably already using underscores in variables formed by two or more words. For example, if you have a variable firstsample, it is recommended to refactor it as first_sample, the reason is readability. It is more easy to figure out that first_sample refers to the first stample than firstsample.

Using underscores instead of spaces for regular English phrases while naming variables is called the Snake case convention in regular Computer Science. Python, however, refers to it as lowercase with words separated by underscores (PEP-8).

Single underscore as prefix

Variable names starting with a single underscore are usually for internal use. An example is _cost.

Function names starting with a single underscore are typically private functions and cannot be accessed from outside the module. An example is _internal_foo().

Single underscore as suffix

Variable names ending with a single underscore can be treated as regular variables. One reason for using variable names ending with an underscore is adding an underscore to a regular Python keyword and creating a new variable out of it. An example is string_.

Double underscore as prefix

Double underscores as prefix are used for name mangling. More about this in a separate article.

Double underscores as prefix and suffix

Python has special methods and variables that start and end with double underscores. They are called magic methods or dunder methods. Examples are __init__() and the ubiquitous __name__. It is best to not create variable names or function names with double underscore prefix and suffixes to avoid issues.

Related Posts

If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.

Disclaimer: Our website is supported by our users. We sometimes earn affiliate links when you click through the affiliate links on our website.

Published on January 20, 2024