TypeError: Can’t Multiply Sequence by Non-Int of Type ‘Float’ — Python Fix Guide
Getting “TypeError: can’t multiply sequence by non-int of type ‘float'” in Python? This guide explains exactly why it happens, where it most commonly appears, and how to fix it with clear code examples.
You write what looks like a simple multiplication in Python, run it, and get this back:
TypeError: can't multiply sequence by non-int of type 'float'
The TypeError: can't multiply sequence by non-int of type 'float' error trips up Python developers at every level. The message sounds technical but the cause is almost always straightforward: somewhere in your code, Python is trying to multiply a string, list, or tuple by a decimal number, and it refuses.
This post explains why that rule exists, the most common situations where it appears, and every fix worth knowing.
Why Python Has This Rule in the First Place
In Python, multiplying a sequence by an integer has a specific meaning: repetition.
"hello" * 3 # "hellohellohello"
[1, 2] * 2 # [1, 2, 1, 2]
("a", "b") * 2 # ('a', 'b', 'a', 'b')
This makes complete sense. Repeating something three times is a whole operation. But repeating something 3.5 times? That has no clear meaning. You can’t have half a copy of a string. Python refuses to guess what you intended, so it raises a TypeError instead.
Floats are excluded from sequence multiplication precisely because repetition requires a whole number of copies. The error is Python’s way of saying: “I know integers go here, and you gave me a float. These are different things.”
This is distinct from arithmetic multiplication. When you multiply two numbers, floats work fine:
3.5 * 2.0 # 7.0
The error only appears when one side of the * operator is a sequence (string, list, or tuple) and the other is a float.
The Most Common Cause: input() Returns a String
The single most frequent trigger for this error is using input() and then trying to use the result in a calculation. In Python 3, input() always returns a string, regardless of what the user types.
quantity = input("Enter quantity: ")
price_per_unit = 1.5
total = quantity * price_per_unit
# TypeError: can't multiply sequence by non-int of type 'float'
Even if the user types 10, quantity holds the string "10", not the number 10. Multiplying "10" by 1.5 hits the error immediately.
The fix: convert the input before using it in math:
quantity = float(input("Enter quantity: "))
price_per_unit = 1.5
total = quantity * price_per_unit
print(total) # 15.0
Wrap input() in float() when you expect a decimal number, or int() when you expect a whole number. Do this at the point of input, not later in your code. That way, the variable holds the right type from the start.
When a Number Is Accidentally Stored as a String
This version of the error is trickier because the variable looks like a number but isn’t. It often happens when reading data from files, CSV readers, or JSON where values come in as strings:
# Data read from a CSV file
rate = "0.08" # still a string
principal = 5000
interest = principal * rate
# TypeError: can't multiply sequence by non-int of type 'float'
rate looks like a float. It has a decimal point. But it’s in quotes, which makes it a string in Python.
The fix:
rate = float("0.08") # 0.08
principal = 5000
interest = principal * rate
print(interest) # 400.0
When reading data from external sources, always convert string values to the appropriate numeric type before calculations. Use float() for decimal values and int() for whole numbers.
You can check what type a variable holds at any point using type():
rate = "0.08"
print(type(rate)) # <class 'str'>
If type() shows str and you expected float, that’s your problem.
Using a Float Where You Need an Integer for Repetition
Sometimes the math in your code produces a float when you actually want to use that result to repeat a sequence. A common example is dividing two numbers to get a count:
total_items = 10
groups = 4
items_per_group = total_items / groups # 2.5 — a float!
separator = "-" * items_per_group
# TypeError: can't multiply sequence by non-int of type 'float'
The / operator in Python 3 always returns a float, even when the result is a whole number (10 / 2 returns 5.0, not 5).
Option 1: Use integer division with // to get a whole number result:
items_per_group = total_items // groups # 2
separator = "-" * items_per_group # "--"
Option 2: Convert the float to an int explicitly:
separator = "-" * int(items_per_group)
Be thoughtful about which one you use. int() truncates (drops the decimal part), while round() rounds to the nearest whole number. If precision matters, use round() then convert:
separator = "-" * round(items_per_group)
NumPy Scalars and Library Returns
This error also shows up when working with NumPy, pandas, or other scientific libraries. These libraries sometimes return scalar values with a type of numpy.float64 rather than Python’s built-in float, and even though they look the same, Python’s sequence multiplication doesn’t accept them.
import numpy as np
multiplier = np.float64(3.0)
result = "abc" * multiplier
# TypeError: can't multiply sequence by non-int of type 'float'
Even though 3.0 is technically a whole number, numpy.float64 is not Python’s int, so the error fires.
The fix: convert to Python’s native int:
result = "abc" * int(multiplier) # "abcabcabc"
When working with values returned by NumPy or pandas operations, wrap them in int() before using them in sequence repetition. This applies to any case where a library might return a non-Python numeric type.
Multiplication vs. Scaling: Know Which One You Need
The root of most occurrences of this error is a confusion between two different operations:
Sequence repetition (creates copies):
"ha" * 3 # "hahaha"
[0] * 5 # [0, 0, 0, 0, 0]
Numeric multiplication (arithmetic):
3.5 * 2.0 # 7.0
If you want arithmetic, both sides must be numbers. If you’re getting this error, one side is a string or sequence that you probably intended to be a number. Convert it first.
A quick diagnostic: when you see this error, add print(type(variable)) for each variable involved in the multiplication. The one that prints <class 'str'> instead of <class 'float'> or <class 'int'> is your culprit.
Handling User Input Safely
When user input drives calculations, it’s good practice to validate and convert in one place rather than assuming the input will always be clean. A basic safe-input pattern:
def get_float_input(prompt):
while True:
try:
return float(input(prompt))
except ValueError:
print("Please enter a valid number.")
price = get_float_input("Enter price: ")
quantity = get_float_input("Enter quantity: ")
total = price * quantity
print(f"Total: {total:.2f}")
This pattern catches invalid input (letters, empty strings) and asks again rather than crashing. In production code, never trust raw input() for calculations without conversion and validation.
Understanding Python’s type system is foundational for writing reliable code. The same attention to types that prevents this error also matters in larger data science contexts. If you’re building Python skills with an eye toward a data career, Get to Know Machine Learning Technology covers where Python fits in the broader machine learning ecosystem.
A Complete Example: Before and After
Before (broken):
hours = input("Enter hours worked: ")
hourly_rate = 25.50
pay = hours * hourly_rate
print(f"Pay: ${pay}")
# TypeError: can't multiply sequence by non-int of type 'float'
After (fixed):
hours = float(input("Enter hours worked: "))
hourly_rate = 25.50
pay = hours * hourly_rate
print(f"Pay: ${pay:.2f}")
# Pay: $204.00 (if user entered 8)
The only change is wrapping input() with float(). That one conversion makes the whole thing work. Data quality and type correctness matter at every scale, from small scripts to enterprise data systems. The importance of clean, correctly typed data is a recurring theme in analytics work, as explored in The Importance of Big Data Analytics in Today’s World on DataWider.
Key Takeaways
The TypeError: can't multiply sequence by non-int of type 'float' error has one cause: you tried to multiply a string, list, or tuple by a float, and Python won’t allow that because sequence multiplication means repetition, which only works with whole numbers.
Here’s the checklist when you hit this error:
- Check what
input()returns — it’s always a string. Wrap it withfloat()orint()before using it in math. - Look for strings that look like numbers — values from files, APIs, or CSV readers often come as strings. Convert them with
float()before calculations. - Check if
/produced a float — in Python 3, division always returns a float. Use//for integer division, or wrap the result inint()orround(). - Check for NumPy or library scalars — convert them to Python’s native
intbefore sequence repetition. - Use
type()to diagnose — print the type of each variable involved in the multiplication to find which one is a string.
The fix is almost always a one-line conversion. The error message is Python’s way of catching a type mismatch before it causes harder-to-debug problems downstream. Keeping clean types in Python is one of the most reliable habits you can build, whether you’re starting with basic scripts or moving toward more complex data work. If you’re just getting started with Python testing and quality, Software Testing for Beginners on DataWider provides a solid starting point for thinking about code reliability.