Last modified: Apr 09, 2025 By Alexander Williams

Fix TypeError: Can't Multiply Sequence by Float

Python developers often encounter the error TypeError: can't multiply sequence by non-int of type 'float'. This error occurs when trying to multiply a sequence (like a list or string) by a float value.

Understanding the Error

The error happens because Python sequences can only be multiplied by integers. Multiplying by a float is not allowed. This is a common issue when working with lists or strings.

For example, trying to multiply a string by 2.5 will raise this error. Strings can only be repeated using whole numbers.


# This will cause the error
text = "Hello"
result = text * 2.5  # Error: can't multiply sequence by float


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

Common Causes

This error typically occurs in these situations:

1. Multiplying a string or list by a float value.

2. Using a float in a repetition operation.

3. Incorrect type conversion in mathematical operations.

Sometimes, this error appears when working with NumPy arrays or Pandas DataFrames. The same rules apply to these data structures.

How to Fix the Error

Here are solutions to resolve this TypeError:

1. Convert Float to Integer

If you need repetition, convert the float to an integer using int():


text = "Hello"
result = text * int(2.5)  # Converts 2.5 to 2
print(result)  # Output: HelloHello

2. Use Proper Data Types

Ensure you're using the correct data types for your operations. For mathematical operations, convert sequences to numbers first.


# Correct approach for numerical multiplication
number = "5"  # This is a string
result = float(number) * 2.5  # Convert to float first
print(result)  # Output: 12.5

3. Check Your Calculations

Review your code for accidental sequence multiplication. This often happens in complex calculations.

If you're getting this error while importing modules, you might want to check our guide on How To Solve ModuleNotFoundError.

Working with Lists

The same error occurs when multiplying lists by floats. Here's how to handle it:


my_list = [1, 2, 3]
# Wrong: result = my_list * 1.5
# Correct:
result = my_list * 2  # Use integer for repetition
print(result)  # Output: [1, 2, 3, 1, 2, 3]

Advanced Example

Here's a more complex example showing proper type handling:


def calculate_area(length_str, width):
    # Convert string to float first
    length = float(length_str)
    return length * width  # Now both are numbers

area = calculate_area("10.5", 2.5)
print(area)  # Output: 26.25

Conclusion

The TypeError: can't multiply sequence by non-int of type 'float' is easy to fix once you understand it. Always check your data types before multiplication operations.

Remember that sequences (strings, lists) can only be multiplied by integers. For numerical operations, convert your data to the proper numeric type first.

For more Python debugging tips, check our other articles on common errors and solutions.