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

Borislav Hadzhiev

Last updated: Apr 8, 2024
8 min

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

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.

main.py
```Copied!```my_str = '2.5'
my_float = 3.1

# โ๏ธ TypeError: can't multiply sequence by non-int of type 'float'
result = my_str * my_float
``````

## #Convert numeric strings to float or int

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.

main.py
```Copied!```my_str = '2'
my_float = 3.1

result = int(my_str) * my_float

print(result)  # ๐๏ธ 6.2
``````

We used the `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.

main.py
```Copied!```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.

IMPORTANT: if you use the `input()` built-in function, all of the values the user enters get converted to strings (even numeric values).

## #You can multiply a string by an integer

Note that you can multiply a string and an integer, but you cannot multiply a string and a float.

main.py
```Copied!```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.

## #Multiply each item in a list by a floating-point number

If you need to multiply each item in a list or a tuple by a number, use a list comprehension.

main.py
```Copied!```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`.

## #Getting input from the user

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

main.py
```Copied!```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.

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

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

main.py
```Copied!```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`.

## #You can multiply a sequence by an integer

Note that multiplying a sequence, such as a list, tuple or string by an integer is valid.

main.py
```Copied!```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.

## #Checking what type the variable stores

If you aren't sure what type of object a variable stores, use the `type()` class.

main.py
```Copied!```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.

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

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.

main.py
```Copied!```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.

## #Multiplying a list by an integer

You could multiply a list (or another sequence) by an integer if you need to duplicate the items in the list N times.

main.py
```Copied!```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.

## #Multiplying each value in a list by a number

If you need to multiply each value in a list by a number, use a list comprehension.

main.py
```Copied!```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`.

List comprehensions are used to perform some operation for every element or select a subset of elements that meet a condition.

## #NumPy makes things more direct and easier

If you use `numpy`, you can achieve the same result in a more direct way.

main.py
```Copied!```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.

When you use the multiplication operator, you have to make sure that at least one of the values is an integer.

If you need to install NumPy, open your terminal in your project's root directory and run the following command.

main.py
```Copied!```pip install numpy

pip3 install numpy
``````

## #Checking what type a variable stores

If you aren't sure what type of object a variable stores, use the `type()` class.

main.py
```Copied!```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.

## #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 integer, e.g. `int(str_1) * int(str_2)`.

Here is an example of how the error occurs.

main.py
```Copied!```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.

## #Convert the strings to integers before multiplying

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
```Copied!```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.

IMPORTANT: if you use the `input()` built-in function, all of the values the user enters get converted to strings (even numeric values).

## #Multiplying a string by an integer

Note that you can also multiply a string by an integer.

main.py
```Copied!```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.

## #Multiply the values in a list by a constant

If you need to multiply values in a list by a number, use a list comprehension.

main.py
```Copied!```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 input() function always returns a string

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

main.py
```Copied!```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
```Copied!```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.

## #Checking what type a variable stores

If you aren't sure what type of object a variable stores, use the `type()` class.

main.py
```Copied!```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.

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

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.

main.py
```Copied!```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.

## #Multiplying a list by an integer is 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.

main.py
```Copied!```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.

main.py
```Copied!```my_tuple = ('a', 'b', 'c')

my_list = list(my_tuple)
print(my_list) # ๐๏ธ ['a', 'b', 'c']
``````

## #How tuples are constructed in Python

In case you declared a tuple by mistake, tuples are constructed in multiple ways:

• Using a pair of parentheses `()` creates an empty tuple
• Using a trailing comma - `a,` or `(a,)`
• Separating items with commas - `a, b` or `(a, b)`
• Using the `tuple()` constructor
Make sure you don't have any trailing commas, e.g. `x = 5,` because that declares a tuple, not an integer.

## #Multiplying each value in a list by a number

If you need to multiply each value in a list by a number, use a list comprehension.

main.py
```Copied!```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`.

When you use the multiplication operator, you have to make sure that at least one of the values is an integer.

## #Checking the type of the variable

If you aren't sure what type of object a variable stores, use the `type()` class.

main.py
```Copied!```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.

## #Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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

Copyright ยฉ 2024 Borislav Hadzhiev