TypeError: can't multiply sequence by non-int of type 'str'

avatar

Borislav Hadzhiev

Last updated: Apr 20, 2022

banner

Check out my new book

TypeError: can't multiply sequence by non-int of type 'str' #

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 int, e.g. int(my_str_1) * int(my_str_2).

typeerror cant multiply sequence by non int of type str

Here is an example of how the error occurs.

main.py
my_str_1 = '5' my_str_2 = '2' # ⛔️ TypeError: can't multiply sequence by non-int of type 'str' result = my_str_1 * my_str_2

If you have a numeric string, you have to convert it to an int (or a float) before multiplying it by a number.

main.py
my_str_1 = '5' my_str_2 = '2' result = int(my_str_1) * int(my_str_2) print(result) # 👉️ 10

We used the int() class to convert the strings to integers.

IMPORTANT: if you use the 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 and an integer.

main.py
my_str_1 = 'abc' my_str_2 = '2' result = my_str_1 * int(my_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.

main.py
my_list = ['1', '2', '3'] result = [int(x) * 2 for x in my_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.

The error often occurs when getting user input using the built-in input() function.

main.py
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.

Even if the user enters a number, it gets converted to a string before it is returned from input().

You can use the int() or float() classes to convert the string to a number.

main.py
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.

main.py
my_str = '3' print(type(my_str)) # 👉️ <class 'str'> print(isinstance(my_str, str)) # 👉️ 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.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.