Various uses of underscore “_” in Python

The underscore has different use cases in different programming languages. In this post, I will show you various uses of “_” in Python.

Uses of “_” in Python

Number formatting

If we use large numbers in Python, it is easier to read if we separate thousands, millions, and billions. The underscore does precisely that in Python. The underscore can be used to make the numbers more readable by developers. For instance, in the following example, we can easily say that the number is one billion because of the use of the underscore.

num = 1_000_000_000

Output: 1000000000

Unpacking tuples

If we are unpacking tuples, we can ignore specific values by using the underscore. For example, let’s say a tuple has three values (1, 2, 3) and we are only interested in the second value, we can ignore the other two values by using the underscore as shown below.

tup = (1, 2, 3)

_, value, _ = tup

Output: 2

Ignoring multiple values of a tuple

If a tuple has a larger number of values like (1, 2, 3, 4, 5, 6), and we are only interested in certain values(say the first and the last), we can combine the underscore and an asterisk to ignore multiple values as shown below.

tup = (1, 2, 3, 4, 5, 6)
a, *_, b = tup

print(a, b)
Output: 1 6

Protected attributes and methods

In Python OOP, there is no concept of protected variables in classes; however, conventionally Python developers have been using underscore in order to separate protected variables from public variables. In the following example, the variable _date_of_birth and the method _get_dob() are considered protected i.e. these are not accessed from outside of the class and its sub-classes.

class Student:
  def __init__(self, name, dob): = name
    self._date_of_birth = dob
  def get_name(self):
  def _get_dob(self):
    return self._date_of_birth
s1 = Student("Robert", "2000-01-01")

print(            # Allowed
print(s1.get_name())      # Allowed
print(s1._get_dob())      # Prohibited conventionally
print(s1._date_of_birth)  # Prohibited conventionally

Note: Python doesn’t actually protects from accessing the variables; it is only convention to not use the variables starting from underscore from other classes.

Private attributes and methods

If you actually want to prevent the usage of the attributes and the variables outside of the class, you have to create private attributes and functions. To do this, you use a double-underscore in front of the name.

class Student:
  def __init__(self, name, dob): = name
    self.__date_of_birth = dob
  def get_name(self):
  def __get_dob(self):
    return self._date_of_birth
s1 = Student("Robert", "2000-01-01")

print(             # Allowed
print(s1.get_name())       # Allowed
print(s1.__get_dob())      # Not allowed by Python
print(s1.__date_of_birth)  # Not allowed by Python

Note: Notice the double underscores

Dunder methods

Dunder methods are special methods in Python with a double underscore on each side of the name. These methods have in-built functionality which can be overwritten. In the following example, the Dunder method __str__ overwrites the in-build functionality in order to make string representation of the objects more readable.

class Book:
  __init__(self, title, author):
    self.title = title = author
    return f"Book: {self.title}"

book = Book("Dune")
Output: Book: Dune

There are so many other Dunder methods that we can use to overwrite in-built functionality. This Reddit post lists all the useful Dunder methods available in Python.

Using reserved names

Let’s say you want to name a variable class for whatever reason. In Python, this name class is a reserved keyword that cannot be used as a variable name or a function name. However, we can add an underscore as a prefix or a suffix to the name in order to use the variable name as shown below.

class_ = "Animalia"
str_ = "Hello"
int_ = 12

Ignoring unimportant values

This is a very rare use case but we can use the underscore instead of a variable name if the value is not important to us as shown in the code below. I have used the “_” in for loop and in the exception handling.

for _ in range(10):
  print("Never gonna give you up")

value = 0
  result = 1 / value
except ZeroDivisionError as _:
  print("Zero division error")



In this post, I showed you different use cases of the underscore “_” in Python. The underscore can be used in number formatting, unpacking tuples, using protected and private variables, Dunder methods, using reserved names, and ignoring unimportant values.

If you have any comments or suggestions regarding this post or any other programming topic, please feel free to post them below.

You can also explore more articles on Programming and Python.


Tech and programming enthusiast

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *