Skip to content

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

Fix the Python TypeError when multiplying sequences (strings, lists, tuples) by non-integer values like floats, lists, or strings with solutions for repetition, element-wise operations, and type conversion.

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:
    result = my_sequence * multiplier
    print(result)
except TypeError as e:
    print(e)

By list

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

try:
    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:
    result = my_sequence * multiplier
    print(result)
except TypeError as e:
    print(e)

By tuple

my_sequence = "Hello"

multiplier = 3, # A tuple (specifically, (3,))

try:
    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"

result_from_float = my_sequence * int(multiplier_float)
print(f"'{my_sequence}' * int({multiplier_float}) = '{result_from_float}'")

result_from_str = my_sequence * int(multiplier_str)
print(f"'{my_sequence}' * int('{multiplier_str}') = '{result_from_str}'") 

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

result = [item * multiplier for item in numeric_list]

print(f"{numeric_list} element-wise * {multiplier} = {result}")

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

import numpy as np

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

np_array = np.array(numeric_list)

result_np = np_array * multiplier

print(f"NumPy array: {np_array}")
print(f"NumPy array * {multiplier} = {result_np}")

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:
    result = str_num1 * str_num2
except TypeError as e:
    print(f"Error: {e}")

result_fixed = float(str_num1) * float(str_num2) # Or int() if appropriate
print(f"float('{str_num1}') * float('{str_num2}') = {result_fixed}")

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}")

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.

Navigation