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

3D illustration of a cloning machine trying to produce two and a half boxes and failing, representing the TypeError multiply sequence by float error.

This is a TypeError multiply sequence that highlights how Python’s multiplication (*) operator works with sequences (like strings and lists).

It means: You can multiply a list or string by an integer (to repeat it), but you can’t multiply it by a float (a decimal number).

⚡ Quick Fix: TypeError: can’t multiply sequence by non-int of type ‘float’ – Python String and List Repetition with Float Conversion Fix

You used a float as the repetition multiplier — Python sequences only accept integers for the * operator, so truncate or round your float first.

import math

float_count = 3.5

# Fix 1 — Truncate with int() (3.5 → 3)
print("A" * int(float_count))   # Output: AAA

# Fix 2 — Round up with math.ceil() (3.5 → 4)
print("A" * math.ceil(float_count))  # Output: AAAA

# Fix 3 — Round to nearest with round() (3.5 → 4)
print([1] * round(float_count))  # Output: [1, 1, 1, 1]

This tells you exactly where your boundary is — now read through the root causes below to find which one broke your code and fix it permanently.

The Cause

Python knows what to do with list * 3, but it has no idea what to do with list * 3.5.

Problem Code (String):

repeat_count = 2.5
my_string = "A" * repeat_count
# CRASH! TypeError: can't multiply sequence by non-int of type 'float'

Problem Code (List):

my_list = [1] * 3    # This is fine! Output: [1, 1, 1]
my_list_broken = [1] * 3.5
# CRASH! TypeError

Python is asking, “How do I give you 3 and a half copies of this list?”

The Fix: Convert to int()

If you are getting a float from a calculation (like an average), you must convert it to an int() before you can use it to repeat a sequence.

float_count = 3.5

# Convert the float to an integer (this will truncate, or "cut off," the .5)
int_count = int(float_count) # int_count is now 3

# Now it works!
my_string = "A" * int_count
print(my_string)
# Output: AAA

If you need to round up instead of down, use math.ceil().

import math
int_count = math.ceil(3.5) # int_count is 4

What This Error Exposes About Python’s Sequence Multiplication Protocol

TypeError: can't multiply sequence by non-int of type 'float' is Python enforcing the __mul__ protocol on sequence types. List and string repetition works by copying the sequence a whole number of times — the interpreter allocates memory for exactly N copies and writes them contiguously. A float multiplier like 3.5 produces a result that has no valid memory layout: half a copy of a list is not a list, and half a copy of a string is not a string. Python refuses to truncate silently and raises the error instead.

The truncation vs rounding distinction matters more than it looks. int(3.9) produces 3, not 4 — it strips the decimal without any rounding logic. That behavior is correct when your business rule says “never exceed the target count,” but wrong when your calculation is a rounded average and cutting off .9 produces a meaningfully different result. math.ceil() always rounds up, round() follows standard half-up rounding, and int() always floors toward zero — pick the one that matches the intent of your original calculation, not just the one that stops the crash.

The most common real-world trigger is a length or count derived from division: total / groups always returns a float in Python 3, even when the division is exact. 10 / 2 returns 5.0, not 5. Use integer division // instead — 10 // 2 returns 5 as a true integer and eliminates the conversion step entirely. Build that habit for any calculation feeding a repetition count, a range() call, or a list index, and this entire error class stops appearing in your code.

Similar Posts

Leave a Reply