Four Usage Scenarios of Underscores in Python

Writing & Translation | Articles & Blog Posts


Underscores in Python have four special usage scenarios. Some of them are conventions to improve readability of code. Some of them have special functions we must know. The four scenarios are:

    No attachments
    • Separate digits of numbers

    This post will talk about all of them to help you totally understand how to use underscores in Python.

    Separate Digits of Numbers

    Sometimes, if a number is too long, it’s hard and boring to count how many digits this number has. For example, if there is a variable a=1000000000000, how many zeros in a?

    Fortunately, Python gives us a very simple way to use underscores as visual separators for digit grouping purposes in integral, floating-point and complex number literals. If we need to define a long number, don’t forget to use underscores to make life easier.

    >>> a = 1_000_000_000_000
    >>> a

    The above statement of a=1_000_000_000_000 is totally the same as a=1000000000000 . Which way do you like to define the a ? ????

    Use to Ignore Some Variable

    Sometimes, a part of values of a container(list/tuple and so on) are useless. We can use an underscore to ignore them.

    >>> L = [1,3,5,7]
    >>> a, _, b, _ = L
    >>> print(a, b)
    1 5

    As the above code shown, we need to get the first and third values of L and assign them to variables a and b. In the meanwhile, the second and last values are useless. We just use _ to ignore them.

    Note: This usage scenario is just a convention. We can use any other names to do this. But using underscore is best choice to avoid ambiguity and let other developers understand it easily.

    Store the Last Expression’s Value in Interpreter

    This is an interesting and small trick of Python Interpreter. We can use one underscore to simply get the last expression’s value.

    >>> 5+6
    >>> _

    Naming Conventions

    Underscores are often used in variable,class and function names. For example:

    first_name = 'Yang'

    When we define the variable first_name , we used a _ to make it more readable.

    Besides using underscores in the middle position of a name, there are four special naming patterns we should know:

    • Single Leading Underscore: _var

    Single/Double Leading Underscore

    In object-oriented programming, there are three types of class members:

    • Public: The public members of a class are accessible from any part of the program. In Python, all member variables and methods without leading underscores are public members by default.
    class Student:
    def __init__(self, name, age, grade):
    self.name = name # public member
    self._age = age # protected member
    self.__grade = grade # private member

    There are two tips about leading underscores:

    • The single leading underscore is just a convention. We can still use it as a public member. But we should not do this. Following good programming conventions will make our code more elegant and easy to be read by other developers.

    Single Trailing Underscore

    Sometimes, we think of a good name for a variable, but unfortunately, the name conflicts with Python keywords or built-ins. We can just use a single trailing underscore to avoid ambiguity. For example:

    >>> list_ = [1,2,3] # good naming style
    >>> list = [2,3,4] # bad name; has ambiguity with Python keyword

    Double Leading and Trailing Underscore

    This convention is used for special variables or methods (so-called “magic method”) of a Python class, such as __init__() method. We just need to know that this convention is used for Python magic methods and avoid naming our variables or methods like this.


    A good developer should follow common code conventions and pay attention to details. Using underscores properly in Python is a good habit.

    Thanks for reading. 

    on June 06th, 2020 (11:32 pm)
    All coments
    This job has not been commented yet.