Skip to main content

How to Resolve Python "TypeError: can't multiply sequence by non-int of type 'X'"

The TypeError: can't multiply sequence by non-int of type 'X' (where 'X' is float, list, str, tuple, etc.) is a Python error indicating an invalid operation involving the multiplication operator (*). Specifically, it occurs when you try to multiply a sequence (like a string, list, or tuple) by something that is not an integer. Python defines sequence multiplication only with integers, meaning repetition.

This guide explains the cause of this error for various non-integer types and provides solutions, including type conversion and alternative approaches for element-wise operations.

Understanding the Error: Sequence Multiplication Rules

In Python, the multiplication operator (*) behaves differently depending on the types involved:

  • Number * Number: Standard mathematical multiplication (e.g., 5 * 2.5 results in 12.5).
  • Sequence * Integer: Repetition. The sequence is repeated integer times (e.g., 'a' * 3 results in 'aaa', [1, 2] * 2 results in [1, 2, 1, 2]).
  • Integer * Sequence: Same as above (repetition).
  • Sequence * Non-Integer (float, list, str, tuple, etc.): Invalid. This operation is not defined and raises the TypeError. Python doesn't know how to "repeat" a sequence a fractional number of times, or repeat it based on another sequence.

Cause: Multiplying Sequence by Non-Integer

The error occurs because one operand is a sequence (str, list, tuple) and the other is something besides an int.

By float

my_sequence = "abc"
multiplier = 2.5 # A float

try:
# ⛔️ TypeError: can't multiply sequence by non-int of type 'float'
result = my_sequence * multiplier
print(result)
except TypeError as e:
print(e)

By list

my_sequence = [1, 2]
multiplier = [3, 4] # A list

try:
# ⛔️ TypeError: can't multiply sequence by non-int of type 'list'
result = my_sequence * multiplier
print(result)
except TypeError as e:
print(e)

By str

my_sequence = ['x', 'y']
multiplier = "2" # A string (even if it looks like a number!)

try:
# ⛔️ TypeError: can't multiply sequence by non-int of type 'str'
result = my_sequence * multiplier
print(result)
except TypeError as e:
print(e)

By tuple

my_sequence = "Hello"

# Note: trailing comma makes it a tuple! multiplier = (3,) is different from multiplier = 3
multiplier = 3, # A tuple (specifically, (3,))

try:
# ⛔️ TypeError: can't multiply sequence by non-int of type 'tuple'
result = my_sequence * multiplier
print(result)
except TypeError as e:
print(e)

Solution 1: Ensure Multiplier is an Integer

If your intent was sequence repetition, make sure the multiplier is an integer.

Using int() Conversion

Convert the non-integer multiplier to an int before the multiplication. Note that converting a float truncates the decimal part.

my_sequence = "abc"
multiplier_float = 2.7
multiplier_str = "3"

# Convert float to int (truncates to 2)
result_from_float = my_sequence * int(multiplier_float)
print(f"'{my_sequence}' * int({multiplier_float}) = '{result_from_float}'")
# Output: 'abc' * int(2.7) = 'abcabc'

# Convert numeric string to int
result_from_str = my_sequence * int(multiplier_str)
print(f"'{my_sequence}' * int('{multiplier_str}') = '{result_from_str}'")
# Output: 'abc' * int('3') = 'abcabcabc'
note

Can not convert list or tuple directly to int:

a = int([3, 4]) # -> TypeError
b = int((3,)) # -> TypeError

Correcting Variable Assignments

Ensure variables intended to be integer multipliers actually hold integers. Pay attention to:

  • Input: input() always returns strings. Use int(input(...)) if an integer is expected.
  • Trailing Commas: x = 5, creates a tuple (5,), not the integer 5. Remove the comma: x = 5.
  • Function Returns: Ensure functions return int where needed.

Solution 2: Element-wise Multiplication (for lists/tuples of numbers)

If your sequence contains numbers and you intended to multiply each number inside the sequence by the multiplier (which could be float, int, etc.), you cannot use the direct * operator on the sequence. Use these instead:

Using List Comprehension

Iterate through the list and multiply each element individually.

numeric_list = [10, 20, 30]
multiplier = 2.5

# ✅ Multiply each element by the multiplier
result = [item * multiplier for item in numeric_list]

print(f"{numeric_list} element-wise * {multiplier} = {result}")
# Output: [10, 20, 30] element-wise * 2.5 = [25.0, 50.0, 75.0]

NumPy allows direct element-wise ("vectorized") operations, which is highly efficient for numerical work.

# Requires: pip install numpy
import numpy as np

numeric_list = [10, 20, 30]
multiplier = 2.5

# ✅ Convert list to NumPy array
np_array = np.array(numeric_list)

# ✅ Multiply the array directly (element-wise)
result_np = np_array * multiplier

print(f"NumPy array: {np_array}")
# Output: NumPy array: [10 20 30]
print(f"NumPy array * {multiplier} = {result_np}")
# Output: NumPy array * 2.5 = [25. 50. 75.]

Solution 3: Convert String to Number (for numeric strings)

If both operands are strings representing numbers, and you intended mathematical multiplication, convert both to int or float.

str_num1 = "10.5"
str_num2 = "3"

try:
# ⛔️ TypeError: can't multiply sequence by non-int of type 'str'
result = str_num1 * str_num2
except TypeError as e:
print(f"Error: {e}")

# ✅ Convert both strings to numbers (e.g., float)
result_fixed = float(str_num1) * float(str_num2) # Or int() if appropriate
print(f"float('{str_num1}') * float('{str_num2}') = {result_fixed}")
# Output: float('10.5') * float('3') = 31.5

Debugging: Check Variable Types (type(), isinstance())

If you're unsure why the multiplication is failing, print the types of both operands right before the operation:

operand1 = [1, 2] # Example list (sequence)
operand2 = 2.5 # Example float (non-int)

print(f"Type of operand1: {type(operand1)}") # Output: <class 'list'>
print(f"Type of operand2: {type(operand2)}") # Output: <class 'float'>

try:
result = operand1 * operand2
except TypeError as e:
print(f"Caught expected error: {e}")
# Output: Caught expected error: can't multiply sequence by non-int of type 'float'

This helps confirm that you have a sequence and a non-integer, guiding you to the appropriate solution.

Conclusion

The TypeError: can't multiply sequence by non-int of type 'X' occurs because Python's * operator only supports multiplying a sequence (string, list, tuple) by an integer (for repetition). Multiplying by floats, lists, strings, tuples, or other non-integer types is not allowed.

To fix this:

  1. If you intended sequence repetition, ensure the multiplier is an integer (use int() to convert if necessary and appropriate).
  2. If you intended element-wise multiplication on a list/tuple of numbers, use a list comprehension or (preferably for performance) NumPy arrays.
  3. If you intended mathematical multiplication between two numeric strings, convert both strings to int or float before multiplying.
  4. Always check variable types if the cause of the error isn't immediately clear.

Understanding the specific behavior of the * operator with different types is key to resolving this error correctly.