Multiplication Operator in Python (*)

The Multiplication Operator (*) is a versatile arithmetic operator in Python. While its primary use is to multiply numeric values, it also plays a powerful role in string and list manipulation through sequence repetition.
1. Numeric Multiplication
Standard multiplication works with integers, floats, and complex numbers. If any operand is a float, the result will always be a float.
# Multiplying Integers
print(5 * 4) # Output: 20
# Multiplying Floats
print(2.5 * 2) # Output: 5.0
# Complex Numbers
print((2 + 3j) * 2) # Output: (4+6j)
2. String and List Repetition
When you multiply a sequence (like a string or a list) by an integer, Python repeats that sequence multiple times. This is incredibly useful for formatting or initializing data.
# String Repetition
print("Hi! " * 3) # Output: Hi! Hi! Hi!
# List Repetition
zeros = [0] * 5
print(zeros) # Output: [0, 0, 0, 0, 0]
3. The Power of Unpacking
In advanced Python, the * operator is used to "unpack" collections into function arguments or new lists.
numbers = [1, 2, 3]
print(*numbers) # Output: 1 2 3 (Unpacked)
4. Common Errors to Avoid
# "Hello" * "World" # Raises TypeError
# "Hello" * 2.5 # Raises TypeError
💡 Interesting Facts
- Booleans: Since
Trueis 1 andFalseis 0,True * 10equals10. - Zero Multiplication: Multiplying any sequence by
0or a negative integer results in an empty sequence (e.g.,""or[]). - Matrix Math: While
*performs element-wise multiplication in some libraries, in standard Python, it follows the rules shown above.