[Solved] Python can’t Multiply Sequence by non-int of type ‘float’

An amazing feature present in python is that we can multiply strings with numbers or any type of data type with other. With python, we can multiply a given the word, a string, or a tuple with a numerical value. Doing so will simply multiply the occurrences of that string. The string will be repeated for an amount equal to the numerical value.

The numerical value can be an integer value but not a floating-point value. Doing so will raise a TypeError. In this article, we shall look into the following TypeError: python can’t multiply sequence by non-int of type ‘float’.

Multiplying string with integer

Unlike other programming languages, python has the feature of multiplying strings with numbers. Doing so creates multiple occurrences of that string. Let us understand with the help of an example. We shall take a string named ‘temp’, which stores the value ‘Python’. We also take another variable, ‘n’, which stores integer value 2. Now, we shall see multiple the variable ‘n’ with ‘temp’.

temp = 'Python'
n = 2
print(temp*n)

The output is:

PythonPython

We can also multiple sequences such as list and tuple and also multiply them with an integer value. The contents of the sequence will be duplicated, and the length of the list would be increased depending on the value of the integer number.

my_list = ['Python', 'Django', 'Flask']
n = 2
print(my_list*n)

The output would be:

['Python', 'Django', 'Flask', 'Python', 'Django', 'Flask']

Similarly, if the value of n is 4, it will multiply the sequence four times.

my_list = ['Python', 'Django', 'Flask']
n = 4
print(my_list*n)

Output:

['Python', 'Django', 'Flask', 'Python', 'Django', 'Flask', 'Python', 'Django', 'Flask', 'Python', 'Django', 'Flask']

Multiplying string with float

If we try to multiply a given string with a float value, it will raise an error because of obvious reasons. You cannot multiply a string with a non – integer value. So, python will raise a TypeError saying that Python Can’t Multiply Sequence By Non-int Of Type ‘float’.

temp = 'Python'
n = 4.0
print(temp*n)

The output will be the below type error thrown:

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

It will raise the same error even if we try to multiply a list with a non – integer value.

my_list = ['Python', 'Django', 'Flask']
n = 4.0
print(my_list*n)

Output:

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

Even though, in this case, the number ultimately equals the integer value 4, yet python considers it as a floating-point number and hence throws the error.

What is TypeError?

TypeError in python is a category of error that is raised by the program when we try to perform an operation on an object of an inappropriate type. The TypeError indicates that the operation we are trying to perform cannot be executed in python. The exception is thrown in various scenarios, such as adding a string to an integer, multiplying a string with a float value, using a string to index a list, etc.

Also, Read | 2 Causes of TypeError: ‘Tuple’ Object is not Callable in Python

How to handle python can’t multiply sequence by non-int of type ‘float’

1. Converting float to integer

The multiplication between float and string is not supported in python. But, to avoid the error from being thrown, we can try converting the given integer value to a string value. We can explicitly convert the number ‘n’ to an integer value before multiplying it with a string value. We achieve that by using the int() function.

It is implemented in the following way:

temp = 'Python'
n = int(4.0)
print(temp*n)

Doing so will treat the number as an integer value and thus remove the error.

PythonPythonPythonPython

When applied on a list sequence, the error shall be resolved in that case too.

my_list = ['Python', 'Django', 'Flask']
n = int(4.0)
print(my_list*n)

Output:

['Python', 'Django', 'Flask', 'Python', 'Django', 'Flask', 'Python', 'Django', 'Flask', 'Python', 'Django', 'Flask']

Also, Read | How to Convert int to float in Python Implicitly and Explicitly

2. Converting numerical string to float

If the string contains a numerical value, we can also convert the string to a floating-point using the float() function. Note that this would be applicable if the string contains only numbers.

temp = '100'
n = 4.0
print(float(temp)*n)

Doing so will multiply the value of the number inside the string with the value ‘n’ and not its occurrences.

400.0

This error may also be thrown in the case where we are trying to accept a numerical input from the user, but we fail to convert it to an integer or a float value.

3. Converting output of input() function to an integer

Let us take a program where we calculate the profit on selling a certain amount of biscuits. The number of biscuits sold is taken as input from the user. The profit for each biscuit is stored inside a variable named ‘profit’. It stores a float value of 75.5. The quantity of biscuits is taken as input with the help of the ‘input()’ function and saved into a variable named ‘sold_count’. Now, when we multiply ‘sold_count’ with ‘profit’, it will raise an error because input() takes the input from the user and stores it in the form of an integer. Thus the multiplication of float with string will raise an error.

profit = 75.5
sold_count = input('Enter the quantity of biscuits sold:')
print(f'The total profit is : {sold_count*profit}')

Output:

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

To solve the error, we will have to convert the ‘sold_count’ into an integer value before multiplying it with the float value ‘profit’. We shall do that by using the int() function.

profit = 75.5
sold_count = int(input('Enter the quantity of biscuits sold:'))
print(f'The total profit is : {sold_count*profit}')

The output is:

Enter the quantity of biscuits sold:20
The total profit is : 1510.0


That sums up everything about Python Can’t Multiply Sequence By Non-int Of Type ‘float’. If you have any doubts, drop your questions in the comments below.

Until next time, Keep Learning!

FAQs

Can Python multiply integer by float?

It can multiply int with float value. It will give you a float result that is more precise. So, decimal will be intact.
Example:
5 * 6.0 is 30.0

Can you multiply an int and a string in Python?

Yes, when a string is multiplied by an int or numeric value, the string will be repeated int number of times or the value which int represents. 
Example:
s = ‘python pool’
print(s * 5)
So output is :
python poolpython poolpython poolpython poolpython pool

Subscribe
Notify of
guest
1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
quizdev
quizdev
2 years ago

Thanks for your answer very clearly. Nice article