What is the purpose of the single underscore _ variable in Python?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The single underscore _ in Python serves multiple purposes depending on context. It is most commonly used as a throwaway variable for values you intentionally ignore, as the last expression result in the interactive interpreter, and as a convention for "private" names with a leading underscore. It is also used in internationalization (i18n) as an alias for gettext. Understanding these conventions makes Python code more readable and idiomatic.
Throwaway Variable
The most common use — indicating a value you do not need:
Using _ signals to other developers that the value is intentionally unused.
Interactive Interpreter Result
In the Python REPL, _ holds the result of the last expression:
This only works in the interactive interpreter, not in scripts.
Loop Variable (Unused Index)
Convention for "Private" Names
A single leading underscore indicates a name is intended for internal use:
The single leading underscore is purely a convention — Python does not enforce access restriction. However, from module import * does not import names starting with _.
Internationalization (i18n)
This is why some codebases use _() for translatable strings — it is a widely recognized convention from GNU gettext.
Digit Separator in Numeric Literals
The underscores are purely visual and do not affect the value.
Pattern Matching Wildcard (Python 3.10+)
In match/case, _ matches any value without binding it to a variable.
Common Pitfalls
- Overwriting
_in loops before using it in the REPL: If you use_as a loop variable, it overwrites the REPL's last-result value. In scripts this does not matter, but in interactive sessions it can be confusing. - Assuming
_prevents the value from being computed:_ = expensive_function()still calls the function and stores the result. The underscore is just a naming convention — it does not optimize anything away. The garbage collector frees it when_is reassigned. - Using
_as a real variable: Some code uses_as an actual meaningful variable (e.g.,_ = some_important_value). This breaks the convention and confuses readers who expect_to be throwaway. Use a descriptive name instead. - Conflicting with gettext in i18n projects: If your project uses
_ = gettext.gettext, using_as a throwaway variable in the same scope shadows the translation function. Use__(double underscore) ordummyas the throwaway name in i18n codebases. - Expecting
_to be truly private: A leading underscore on class attributes (_name) is a convention, not enforcement. Any code can still accessobj._name. Only name mangling with double underscores (__name) provides any protection, and even that can be bypassed.
Summary
_as a throwaway variable signals that a value is intentionally ignored- In the Python REPL,
_stores the result of the last expression - Use
_infor _ in range(n)when the loop counter is unused - A leading
_on names (_var,_method) indicates internal/private by convention _is the wildcard pattern in Python 3.10+match/casestatements- Underscores in numeric literals (
1_000_000) are visual separators with no effect on the value

