Multiplication in Python is done with the * operator: 6 * 7 is 42. It works on integers, floats and complex numbers, and when one side is a whole number and the other is a string or list, it repeats that sequence instead.
Lines starting with >>> below are example output from a Python prompt. Error text can differ slightly between Python versions.
Multiply two numbers
Put * between the two values. The language reference says the arguments are converted to a common real type and then multiplied, so mixing an int and a float gives a float:
>>> 6 * 7
42
>>> 2 * 1.5
3.0
>>> -4 * 5
-20
>>> -4 * -5
20
>>> 0 * 123456
0
Integers in Python have no fixed size limit, so large products do not overflow. 2 ** 100 * 3 simply returns a longer integer. The multiplication sign you may know from maths class, the letter x, is not an operator. Writing 6 x 7 is a SyntaxError, and leaving the operator out as in 6(7) fails too, because Python reads the parentheses as a function call. Python never assumes multiplication between two values.
Multiply variables and use *=
Names work exactly like the numbers they hold:
price = 19.99
quantity = 3
total = price * quantity
print(total) # 59.97
To update a variable in place, use the augmented assignment form. x *= n is shorthand for x = x * n:
>>> balance = 100
>>> balance *= 3
>>> balance
300
The same pattern exists for the other arithmetic operators, such as += and -=.
Order of operations with *
In the grammar of the reference, * is grouped below + and -, so multiplication happens first, as in ordinary arithmetic. The power operator ** binds tighter than *:
>>> 2 + 3 * 4
14
>>> (2 + 3) * 4
20
>>> 2 * 3 ** 2
18
* shares its level with /, //, % and @, and operators at the same level run left to right. Add parentheses whenever a reader could doubt the order. For the neighbouring operators, see division and floor division in Python, the modulo operator and the exponent operator and pow().
Multiplying floats gives rounding noise
Floats are stored in binary, so many decimal values are only approximations. Multiplication can expose that:
>>> 0.1 * 3
0.30000000000000004
>>> 1.1 * 3
3.3000000000000003
This is not a bug in the multiplication. The inputs were already slightly off. For display, round the result with round(value, 2) or an f-string such as f"{value:.2f}". For money, use the decimal module, where the values are decimal and exact:
>>> from decimal import Decimal
>>> Decimal("0.1") * 3
Decimal('0.3')
Build a Decimal from a string, not from a float, or you copy the float's error into it. More on this in the Python float type explained.
Multiply complex numbers
Complex numbers multiply by the usual algebra, with j as the imaginary unit:
>>> (1 + 2j) * (3 + 4j)
(-5+10j)
That is 3 + 4j + 6j + 8j*j, and j*j is -1, which gives -5 + 10j. The reference also notes a change in Python 3.14: if only one operand is complex, the other is converted to a floating-point number.
Repeat a string or list with *
The reference says that when one argument is an integer and the other is a sequence, sequence repetition is performed. The integer can be on either side:
>>> "ab" * 3
'ababab'
>>> 3 * "ab"
'ababab'
>>> [0] * 5
[0, 0, 0, 0, 0]
>>> (1, 2) * 2
(1, 2, 1, 2)
>>> "-" * 20
'--------------------'
This is the usual way to draw a divider line or to create a list of 5 zeros. A repeat count of zero or less gives an empty sequence of the same type. The reference puts it as "a negative repetition factor yields an empty sequence", and the sequence docs say values less than 0 are treated as 0:
>>> "ab" * 0
''
>>> [1, 2] * -1
[]
The nested list trap
The docs warn that items in the repeated sequence are not copied; they are referenced multiple times. With numbers and strings that does not matter, because they cannot be changed in place. With a list of lists it does:
>>> lists = [[]] * 3
>>> lists[0].append(3)
>>> lists
[[3], [3], [3]]
All three elements are the same inner list. To get three separate lists, build them in a comprehension, as the documentation shows:
>>> lists = [[] for _ in range(3)]
>>> lists[0].append(3)
>>> lists
[[3], [], []]
The same rule applies to a grid. Use [[0] * 3 for _ in range(2)] for two rows of three zeros. Each row is a fresh list, and the inner [0] * 3 is safe because the zeros are immutable. If comprehensions are new to you, see Python list comprehension.
Multiply every number in a list
The * operator takes two operands. To multiply all items of a list, use math.prod():
>>> import math
>>> math.prod([1, 2, 3, 4])
24
>>> math.prod([])
1
The docs say the default start value is 1 and that an empty iterable returns the start value. Hence the 1 for an empty list. The function is intended for numbers and may reject other types, so do not use it to join strings.
To multiply two lists item by item, pair them with zip():
>>> prices = [2.0, 3.5, 10.0]
>>> counts = [4, 2, 1]
>>> [price * count for price, count in zip(prices, counts)]
[8.0, 7.0, 10.0]
Writing prices * counts would raise a TypeError, because neither list is an integer. The same goes for [1, 2] * 2.0: the repeat count must be an integer, not a float.
Matrix multiplication with @
Python has a separate operator for matrix multiplication, @, added in Python 3.5. The reference is blunt about its scope: no built-in Python type implements it. It exists for libraries. With NumPy installed, the two operators behave differently:
>>> import numpy as np
>>> a = np.array([[1, 2], [3, 4]])
>>> b = np.array([[5, 6], [7, 8]])
>>> a * b
array([[ 5, 12],
[21, 32]])
>>> a @ b
array([[19, 22],
[43, 50]])
Here a * b multiplies matching elements, and a @ b is the matrix product, where entry (0, 0) is 1*5 + 2*7 = 19. NumPy is a third-party library and is not part of this article's sources, so check its documentation for details.
Multiplying values from input()
input() returns a string, and a string times an integer repeats. This is a classic beginner surprise:
>>> quantity = "3"
>>> quantity * 2
'33'
>>> int(quantity) * 2
6
Convert with int() or float() before multiplying. Both raise ValueError for text that is not a number, so handle that case if the input comes from a user.
Make * work for your own class
The reference says the operation can be customized with __mul__() and __rmul__(). Python calls __mul__ on the left operand first, and __rmul__ on the right operand when the left one cannot handle it:
class Money:
def __init__(self, cents):
self.cents = cents
def __mul__(self, factor):
return Money(round(self.cents * factor))
__rmul__ = __mul__
def __repr__(self):
return f"Money({self.cents})"
print(Money(250) * 3) # Money(750)
print(3 * Money(250)) # Money(750)
Without the __rmul__ line, 3 * Money(250) would fail, because int does not know how to multiply by a Money.
Errors you may hit
TypeErrorwhen multiplying two strings:"a" * "b"fails with a message likecan't multiply sequence by non-int of type 'str'. A sequence can only be multiplied by an integer. Convert one side withint().TypeErrorwith a float count:"ab" * 2.5fails for the same reason. Useint(2.5)if you really want to repeat twice.TypeErrorwithNone: a function without areturnstatement givesNone, andNone * 2fails. Check what the function actually returns.- A string where you expected a number: see the
input()section above. - Shared inner lists: changing one row changes all rows. See the nested list trap.
Frequently asked questions
- What is the multiplication symbol in Python?
- The asterisk, *. Write 6 * 7 to get 42. The x character and the dot do not work as multiplication operators, and a missing operator such as 6(7) is an error because Python does not assume multiplication between values.
- How do I multiply all the numbers in a list in Python?
- Use math.prod(numbers). For [1, 2, 3, 4] it returns 24. Per the Python documentation, an empty iterable returns the start value, which is 1 by default. The function is meant for numbers and may reject other types.
- What does * do with a string or a list?
- If one operand is an integer and the other is a sequence, * repeats the sequence. 'ab' * 3 gives 'ababab' and [0] * 3 gives [0, 0, 0]. A zero or negative count gives an empty sequence of the same type.
- Does Python have a matrix multiplication operator?
- Yes, @, added in Python 3.5. The documentation says no built-in Python type implements it, so it only works with types that define __matmul__(), such as NumPy arrays. Using * on NumPy arrays multiplies element by element instead.
- Why does 0.1 * 3 give 0.30000000000000004?
- Floats are stored in binary, and 0.1 has no exact binary representation. The product is the closest float to the stored approximation, which prints with extra digits. Round for display, or use the decimal module when you need exact decimal arithmetic.