Last updated: Apr 8, 2024
Reading timeยท8 min
The Python "TypeError: can't multiply sequence by non-int of type 'float'" occurs when we try to multiply a sequence (e.g. a string or a list) by a float.
To solve the error, convert the string to a number before multiplying it, e.g.
float(my_str) * 3.14
.
Here is an example of how the error occurs.
my_str = '2.5' my_float = 3.1 # โ๏ธ TypeError: can't multiply sequence by non-int of type 'float' result = my_str * my_float
If you have a numeric string, you have to convert it to a float
or an int
before multiplying it by a floating-point number.
my_str = '2' my_float = 3.1 result = int(my_str) * my_float print(result) # ๐๏ธ 6.2
int()
class to convert the string to an integer before using the multiplication operator.If you need to convert the string to a floating-point number, use the float() class instead.
my_str = '2.5' my_float = 3.1 result = float(my_str) * my_float print(result) # ๐๏ธ 7.75
The float class takes a string and converts the given string to a floating-point number.
input()
built-in function, all of the values the user enters get converted to strings (even numeric values).Note that you can multiply a string and an integer, but you cannot multiply a string and a float.
my_str = 'ab' my_float = 3.14 print(my_str * int(my_float)) # ๐๏ธ "ababab"
When a string is multiplied by an integer, it is repeated N times.
Notice that we used the int()
class to convert the floating-point number to an
integer before multiplying.
If you need to multiply each item in a list or a tuple by a number, use a list comprehension.
my_list = [1.1, 2.2, 3.3] my_float = 2.2 result = [x * my_float for x in my_list] # ๐๏ธ [2.4200000000000004, 4.840000000000001, 7.26] print(result)
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.
We used a list comprehension to multiply each item in the list by 2.2
.
The error often occurs when getting user input using the built-in input()
function.
num = input('Enter your fav number: ') print(num) # ๐๏ธ '3.14' print(type(num)) # ๐๏ธ <class 'str'> # โ๏ธ TypeError: can't multiply sequence by non-int of type 'float' result = num * 2.2
The input function converts the data to a string and returns it.
input()
.You can use the float()
or int()
classes to convert the string to a number.
num = input('Enter your fav number: ') print(num) # ๐๏ธ '3.14' result = float(num) * 2.2 print(result) # ๐๏ธ 6.908000000000001
We converted the string to a floating-point number before multiplying by 2.2
.
Note that multiplying a sequence, such as a list, tuple or string by an integer is valid.
my_list = ['a', 'b'] my_float = 3.14 result = my_list * int(my_float) print(result) # ๐๏ธ ['a', 'b', 'a', 'b', 'a', 'b']
However, you cannot multiply a sequence by a float.
If you aren't sure what type of object a variable stores, use the type()
class.
my_list = ['a', 'b'] print(type(my_list)) # ๐๏ธ <class 'list'> print(isinstance(my_list, list)) # ๐๏ธ True my_float = 3.14 print(type(my_float)) # ๐๏ธ <class 'float'> print(isinstance(my_float, float)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance() function
returns True
if the passed-in object is an instance or a subclass of the
passed-in class.
The Python "TypeError: can't multiply sequence by non-int of type list" occurs
when we try to multiply a sequence (e.g. a list or a string) by a list
object.
To solve the error, correct the assignment to an int to be able to use the multiplication operator.
Here is an example of how the error occurs.
my_list_1 = [1, 2, 3] my_list_2 = [4, 5, 6] # โ๏ธ TypeError: can't multiply sequence by non-int of type 'list' result = my_list_1 * my_list_2
We are trying to multiply a list by another list which is not allowed.
You could multiply a list (or another sequence) by an integer if you need to duplicate the items in the list N times.
my_list_1 = [1, 2, 3] result = my_list_1 * 2 print(result) # ๐๏ธ [1, 2, 3, 1, 2, 3]
Multiplying a list by an integer duplicates the elements in the list N times.
If you need to multiply each value in a list by a number, use a list comprehension.
my_list = [1, 2, 3] result = [x * 2 for x in my_list] print(result) # ๐๏ธ [2, 4, 6]
We used a list comprehension to multiply each item in the list by 2
.
If you use numpy
, you can achieve the same result in a more direct way.
import numpy as np arr = np.array([1, 2, 3]) result = arr * 2 print(result) # ๐๏ธ [2, 4, 6]
You can directly multiply a NumPy array with an integer to multiply each item in the array.
If you need to install NumPy, open your terminal in your project's root directory and run the following command.
pip install numpy pip3 install numpy
If you aren't sure what type of object a variable stores, use the type()
class.
my_list = [1, 2, 3] print(type(my_list)) # ๐๏ธ <class 'list'> print(isinstance(my_list, list)) # ๐๏ธ True my_int = 5 print(type(my_int)) # ๐๏ธ <class 'int'> print(isinstance(my_int, int)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance() function
returns True
if the passed-in object is an instance or a subclass of the
passed-in class.
The Python "TypeError: can't multiply sequence by non-int of type 'str'" occurs when we try to multiply a sequence (e.g. a string or a list) by a string.
To solve the error, convert the string to a float or an integer, e.g.
int(str_1) * int(str_2)
.
Here is an example of how the error occurs.
str_1 = '5' str_2 = '2' # โ๏ธ TypeError: can't multiply sequence by non-int of type 'str' result = str_1 * str_2
Trying to multiply a sequence by a string causes the error.
If you have a numeric string, you have to convert it to an int
(or a float
)
before multiplying it by a number.
str_1 = '5' str_2 = '2' result = int(str_1) * int(str_2) print(result) # ๐๏ธ 10
We used the int()
class to convert the strings to integers.
input()
built-in function, all of the values the user enters get converted to strings (even numeric values).Note that you can also multiply a string by an integer.
str_1 = 'abc' str_2 = '2' result = str_1 * int(str_2) print(result) # ๐๏ธ "abcabc"
Make sure that at least 1 of the values is an integer before using the multiplication operator.
If you need to multiply values in a list by a number, use a list comprehension.
list = ['1', '2', '3'] result = [int(x) * 2 for x in list] print(result) # ๐๏ธ [2, 4, 6]
We used a list comprehension to convert each string in the list to an integer
and multiply it by 2
.
List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.
The error often occurs when getting user input using the built-in input()
function.
num1 = input('Enter num 1: ') print(num1) # ๐๏ธ '5' num2 = input('Enter num 2: ') print(num2) # ๐๏ธ '5' # โ๏ธ TypeError: can't multiply sequence by non-int of type 'str' result = num1 * num2
The input function converts the data to a string and returns it.
input()
.You can use the int()
or float()
classes to convert the string to a number.
num1 = input('Enter num 1: ') print(num1) # ๐๏ธ '5' num2 = input('Enter num 2: ') print(num2) # ๐๏ธ '5' result = int(num1) * int(num2) print(result) # ๐๏ธ 25
We converted both strings to integers before multiplying them.
If you aren't sure what type of object a variable stores, use the type()
class.
str = '3' print(type(str)) # ๐๏ธ <class 'str'> print(isinstance(str, str)) # ๐๏ธ True int = 5 print(type(int)) # ๐๏ธ <class 'int'> print(isinstance(int, int)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance() function
returns True
if the passed-in object is an instance or a subclass of the
passed-in class.
The Python "TypeError: can't multiply sequence by non-int of type tuple"
occurs when we try to multiply a sequence (e.g. a list or a string) by a tuple
object.
To solve the error, correct the assignment to an int to be able to use the multiplication operator.
Here is an example of how the error occurs.
my_list = [1, 2] my_tuple = 3, # โ๏ธ TypeError: can't multiply sequence by non-int of type 'tuple' print(my_list * my_tuple)
We are trying to multiply a list by a tuple which is not allowed.
You could multiply a list (or another sequence, e.g. a string or a tuple) by an integer if you need to duplicate the items in the list N times.
my_list = [1, 2] my_int = 3 # ๐๏ธ [1, 2, 1, 2, 1, 2] print(my_list * my_int) print('abc' * 2) # ๐๏ธ 'abcabc'
When we multiply a list by an integer, we duplicate the items of the list N times.
You can use the list() class if you need to convert a tuple to a list.
my_tuple = ('a', 'b', 'c') my_list = list(my_tuple) print(my_list) # ๐๏ธ ['a', 'b', 'c']
In case you declared a tuple by mistake, tuples are constructed in multiple ways:
()
creates an empty tuplea,
or (a,)
a, b
or (a, b)
tuple()
constructorx = 5,
because that declares a tuple, not an integer.If you need to multiply each value in a list by a number, use a list comprehension.
my_list = [1, 2, 3] result = [x * 2 for x in my_list] print(result) # ๐๏ธ [2, 4, 6]
We used a list comprehension to multiply each item in the list by 2
.
If you aren't sure what type of object a variable stores, use the type()
class.
my_tuple = (1, 2, 3) print(type(my_tuple)) # ๐๏ธ <class 'tuple'> print(isinstance(my_tuple, tuple)) # ๐๏ธ True my_int = 10 print(type(my_int)) # ๐๏ธ <class 'int'> print(isinstance(my_int, int)) # ๐๏ธ True
The type class returns the type of an object.
The isinstance() function
returns True
if the passed-in object is an instance or a subclass of the
passed-in class.
You can learn more about the related topics by checking out the following tutorials: