fix typeerror can't multiply sequence by non-int of type 'float' in python

How to Fix – TypeError can’t multiply sequence by non-int of type ‘float’

In Python, the TypeError: can't multiply sequence by non-int of type 'float' error occurs when you try to multiply a sequence type (for example, string, list, tuple, etc.) with a non-integer value. This error can be frustrating, especially if you are new to Python programming. In this tutorial, we’ll dive deep into the common scenarios in which this error can error and how to fix it.

fix typeerror can't multiply sequence by non-int of type 'float' in python

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

In Python, a sequence is a collection of ordered and indexed elements. The most common sequence types in Python are lists, tuples, and strings. Python allows you to multiply sequences by integer values to create a new sequence that repeats the original sequence.

Let’s look at an example.

# create a string
s = "hello"
print(s * 3)

Output:

hellohellohello

In the above example, we multiply the string s with the integer 3. You can see that the resulting value is the string s repeated 3 times.

You can similarly multiply other sequence types such as lists and tuples with integer values.

# create a list
ls = [1, 2, 3]
print(ls * 3)

Output:

[1, 2, 3, 1, 2, 3, 1, 2, 3]
# create a tuple
tup = ('cat', 'dog', 'bunny')
print(tup * 2)

Output:

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

('cat', 'dog', 'bunny', 'cat', 'dog', 'bunny')

Reproducing the error

In the above examples, we saw that we can multiply sequences with integer values to get repeated sequences. But, if you multiply a sequence with a float value, etc. You’ll get an error.

Let’s look at an example.

# create a string
s = "hello"
# multiply string with a float value
print(s * 3.0)

Output:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Cell In[9], line 4
      2 s = "hello"
      3 # multiply string with a float value
----> 4 print(s * 3.0)

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

Here, we multiplied the s with a float value 3.0, on running the code we get the error, TypeError: can't multiply sequence by non-int of type 'float'.

You’ll get this error on multiplying other sequence types with a float value as well.

# create a list
ls = [1, 2, 3]
# multiply list with a float value
print(ls * 3.0)

Output:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Cell In[10], line 4
      2 ls = [1, 2, 3]
      3 # multiply list with a float value
----> 4 print(ls * 3.0)

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

Output:

# create a tuple
tup = ('cat', 'dog', 'bunny')
# multiply tuple with a float value
print(tup * 2.0)

Output:

---------------------------------------------------------------------------

TypeError                                 Traceback (most recent call last)

Cell In[11], line 4
      2 tup = ('cat', 'dog', 'bunny')
      3 # multiply tuple with a float value
----> 4 print(tup * 2.0)

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

Why does this error occur?

In the above examples, we saw that multiplying a sequence type in Python with a float value with result in a TypeError: can't multiply sequence by non-int of type 'float'. This is because you can only multiply a sequence type with an integer and not any other type.

Fixing the error

To fix this error, instead of multiplying the sequence with a float type value, multiply it with an integer type value. You can use the int() method to cast a float value to an integer type in Python.

Let’s fix the errors in the above example using this method.

# create a string
s = "hello"
# multiply string with an int value
print(s * int(3.0))

Output:

hellohellohello
# create a list
ls = [1, 2, 3]
# multiply list with an int value
print(ls * int(3.0))

Output:

[1, 2, 3, 1, 2, 3, 1, 2, 3]
# create a tuple
tup = ('cat', 'dog', 'bunny')
# multiply tuple with an int value
print(tup * int(2.0))

Output:

('cat', 'dog', 'bunny', 'cat', 'dog', 'bunny')

You can see that we don’t get that error now since we are first converting the float values to int and then multiplying the sequence with the resulting integer value.

Conclusion

The TypeError: can't multiply sequence by non-int of type 'float' error occurs when you try to multiply a sequence type in Python (for example, list, tuple, string) with a float value. You can only multiply sequences with integer values in Python which will result in repeated sequences. To fix this error, you need to convert the float value to an integer value. You can use the int() method to convert a float value to an int value.

You might also be interested in –

Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top