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

The TypeError: can’t multiply sequence by non-int of type ‘float’ is due to the multiplication operator is used between a string and a float number in python. The string can’t multiply sequence by non-int of type float. The multiply sequence is created between a string and an integer value in python. If the multiply sequence is created between a string and a float decimal number, the python interpreter will throw this TypeError: can’t multiply sequence by non-int of type ‘float’ error.

In python, if the multiplication operator is used between a string and a number, the python interpreter repeats the string for ‘n’ times. The number must be an integer. If the multiplication operator is used between a string and a float number, the python interpreter can not repeat the string and throws the type error.

The Error TypeError: can’t multiply sequence by non-int of type ‘float’ is because the repeat count is not available while multiplying the string in python.



Exception

The error will be seen as shown in the below stack trace. The stack trace of the error will show the line at which the error occurred. In the example below the error occurred in line 3.

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print x * y
TypeError: can't multiply sequence by non-int of type 'float'


Root Cause

The multiplication operator is used for repeating the string ‘n’ times between a string and a number. If the multiplication operator is used between a string and a float number, the python interpreter treats a string as a repeated string and considers an integer to be an another one. As a result, the python interpreter throws a type error saying that there is a non-integer instead of an integer.



How to reproduce this error

If you multiple a string with a float number, the string cannot multiple with a decimal number. The string can be multiplied with an integer number or an integer number should be multiplied with a float number. The error can be reproduced if a string is multiplied with a float number.

Program

x = 5.0
y = "Yawin Tutor "
print x * y

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print x * y
TypeError: can't multiply sequence by non-int of type 'float'
[Finished in 0.0s with exit code 1]


Solution 1

The error TypeError: can’t multiply sequence by non-int of type ‘float’ is due to the multiplication operator ( * ) is used between a string and a float number. If the float number is changed to an integer number, This issue will be resolved.

In the example below, a string is multiplied with an integer number. If a string is multiplied with an integer number, the string will be repeated for n times. The string “Yawin Tutor” is repeated for 5 times.

x = 5
y = "Yawin Tutor "
print x * y

Output

Yawin Tutor Yawin Tutor Yawin Tutor Yawin Tutor Yawin Tutor 
[Finished in 0.1s]


Solution 2

The string can not be multiplied with a float number. The string may contain a number. The number string must be converted to an integer number or a float number. The number can be multiplied with a float number.

Program

x = 5.0
y = "10"
print x * y

Error

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print x * y
TypeError: can't multiply sequence by non-int of type 'float'

Solution

x = 5.0
y = "10"
print x * int(y)

Output

50.0
[Finished in 0.0s]


Solution 3

If the string contains a float value, you can not convert to integer. The decimal value of the float will be truncated. The float() function is used to convert a string to a float number. The string should be converted as a float number using float() function before multiply with an another float number.

x = 5.0
y = "10.3"
print x * y

Error

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print x * y
TypeError: can't multiply sequence by non-int of type 'float'

Solution

x = 5.0
y = "10.3"
print x * float(y)

Output

51.5
[Finished in 0.0s]



Leave a Reply