Skip to main content
The * (asterisk) operator in Python is a heavily overloaded lexical token whose behavior is dictated by its syntactic context. It functions primarily as a binary operator for arithmetic and sequence operations, a unary prefix operator for iterable unpacking, a syntax marker in function signatures for variadic arguments, a modifier in exception handling for exception groups, and a wildcard identifier in module imports.

Arithmetic Multiplication and Sequence Repetition

When used as a binary operator, * evaluates the operands by invoking the __mul__ or __rmul__ magic methods.
  • Numeric Types: Performs standard mathematical multiplication.
  • Sequence Types: When one operand is a sequence (e.g., list, tuple, str) and the other operand is an integer, it performs sequence repetition. This operation is commutative; it creates a new sequence containing multiple concatenated shallow copies of the original items regardless of operand order.

Iterable Unpacking

As a unary prefix operator, * unpacks an iterable into its constituent elements. This behavior is defined by PEP 3132 (Extended Iterable Unpacking) and PEP 448 (Additional Unpacking Generalizations).
  • Assignment Unpacking: Captures all remaining items in an iterable that are not assigned to explicit variables, packing them into a list. Only one starred expression is permitted in a single assignment target.
  • Literal Displays: Expands iterables directly within list, tuple, or set literals.

Function Signatures: Packing and Unpacking

In the context of function definitions and function invocations, * manages the mapping of positional arguments.
  • Parameter Packing (Variadic Positional Arguments): When prefixed to a parameter in a function definition, * collects an arbitrary number of unbound positional arguments into a tuple.
  • Argument Unpacking: When prefixed to an iterable in a function call, * exhausts the iterable, passing its elements as individual positional arguments.
  • Keyword-Only Argument Enforcement: When used as a standalone parameter (a bare *) in a function definition, it consumes no arguments but dictates that all subsequent parameters must be passed as keyword arguments.

Exception Group Handling

Introduced in Python 3.11 (PEP 654), the * token is appended to the except keyword to form the except* clause. This syntax is specifically designed to catch and unpack ExceptionGroup instances, allowing multiple exceptions of different types raised concurrently to be handled by separate except* blocks.

Wildcard Imports

In the context of the import statement, * acts as a wildcard identifier. It binds all public names defined in the target module to the current local namespace. The definition of “public names” is determined by the module’s __all__ list; if __all__ is undefined, it imports all names that do not begin with an underscore (_).
Tired of Poor Python Skills? Fix That With Deep Grasping!Learn More