Python throws typeerror: can’t multiply sequence by non-int of type ‘float’ when you try to multiply a string with float value. Although you can multiply a string with int value but except that no other type is supported.

We get this situation when we take input from user and do computation over it. The inputs are always in string type, no matter if a digit is entered, it will be treated as string. It’s your job to typecast it to your required type like in this case, its float.

## Code Example

A sequence could be a list or a string of characters. Few examples of sequence are –

- [1, 2, 3, 4, 5, 6]
- “Ironman”
- “24.36”

All of the above formats are sequences and any of them can generate typeerror if they are multiplied by any type other than int. But why not with int? This is because Python has a definition for an output that’s get generated by the multiplication of string and int. The same was not possible with float or any other type. For example –

>>> "Ironman" * 3 Output: "IronmanIronmanIronman" >>> "24.36" * 3 Output: "24.3624.3624.36" >>> 24.36 * 3 Output: 73.08

If instead of int, we multiply string with float, we will get typeerror: can’t multiply sequence with non-int type –

>>> "24.36" * 3.0 Output: TypeError: Can't multiply sequence by non-int of type 'float'

Hope you got the difference. Buy, why would we multiply a string with float? Generally, we don’t but there are situations where we overlook like an output returned by a library or a function created by someone else and we don’t know the return type. Similarly, when we take the input from user and assumes that the value entered will be float, but the values are always strings. Check this code –

>>> myNumber = raw_input (["Insert a number here \n"]) >>> myNumber * 3.0 Output: Insert a number here 5.2 TypeError: Can't multiply sequence by non-int of type 'float'

To solve this issue, you need to typecast the entered value. Convert the raw input to the float type –

>>> myNumber = float(raw_input (["Insert a number here \n"])) >>> myNumber * 3.0 Output: Insert a number here 5.2 15.6

Suppose, there is another sequence like list. What will happen if you multiply it with float value? You will get typeerror –

>>> [1.0, 2.0, 3.0] * 3.0 Output: TypeError: Can't multiply sequence by non-int of type 'float'

You can solve this by two methods – By converting list to `numpy`

array or by using `for`

statement –

>>> import numpy as np >>> np.asarray([1.0, 2.0, 3.0]) * 3.0 Output: [3.0, 6.0, 9.0]

>>> [i * 3.0 for i in [1.0, 2.0, 3.0]] Output: [3.0, 6.0, 9.0]