Last modified: Apr 25, 2026 By Alexander Williams
Fix: Can't Multiply Sequence by Non-Int Str
Python is a powerful language, but it can be strict with data types. One common error beginners face is TypeError: can't multiply sequence by non-int of type 'str'. This error happens when you try to multiply a string, list, or tuple by another string. Python only allows multiplication of a sequence by an integer.
In this article, you will learn why this error occurs. You will see clear examples and solutions. By the end, you will know how to fix it and avoid it in the future. This guide is perfect for beginners and anyone who wants to write cleaner Python code.
What Does This Error Mean?
The error message tells you exactly what is wrong. You tried to multiply a sequence (like a string or list) by a non-integer value. In this case, the non-integer is a string. Python's multiply operator (*) works differently for sequences. It repeats the sequence a given number of times, but only if the multiplier is an integer.
For example, "hello" * 3 returns "hellohellohello". But "hello" * "3" will raise this error because "3" is a string, not an integer.
Common Causes of the Error
This error usually appears when you mix data types by accident. Here are the most common scenarios:
- You read a number from user input, but it is stored as a string.
- You try to multiply a string by a variable that contains a string.
- You use the wrong variable in a calculation.
- You forget to convert a string to an integer or float.
Example 1: Multiplying a String by a String
The simplest case is when you try to multiply two strings directly.
# This will cause the error
result = "Python" * "3"
print(result)
TypeError: can't multiply sequence by non-int of type 'str'
The fix is simple. Convert the second string to an integer using the int() function.
# Correct version
result = "Python" * int("3")
print(result)
PythonPythonPython
Example 2: User Input as a String
User input is always a string in Python. This is a very common source of this error.
# User input example
name = "Alice"
repeat = input("How many times? ") # User types 5
print(name * repeat) # Error!
TypeError: can't multiply sequence by non-int of type 'str'
To fix it, convert the input to an integer.
# Fixed version
name = "Alice"
repeat = int(input("How many times? ")) # Convert to int
print(name * repeat)
AliceAliceAliceAliceAlice
Example 3: Multiplying a List by a String
Lists are also sequences. The same rule applies. You cannot multiply a list by a string.
# List example
my_list = [1, 2, 3]
multiplier = "2"
print(my_list * multiplier) # Error
TypeError: can't multiply sequence by non-int of type 'str'
Again, convert the string to an integer.
# Fixed version
my_list = [1, 2, 3]
multiplier = int("2")
print(my_list * multiplier)
[1, 2, 3, 1, 2, 3]
How to Prevent This Error
Prevention is better than fixing. Here are some tips to avoid this error in your code:
- Always check the type of your variables before using them in multiplication.
- Use the
type()function to debug and see what type a variable holds. - Convert user input to the correct type immediately after reading it.
- Use
isinstance()to verify that a variable is an integer before multiplying.
If you are working with more complex code, you might encounter other type errors. Check out our guide on Python TypeError: Causes and Fixes for a broader overview of common type errors and how to solve them.
Using Type Conversion Functions
Python provides built-in functions to convert data types. The most important ones for this error are int() and float(). Use int() when you want a whole number. Use float() when you need decimal precision.
But be careful. If the string cannot be converted to a number, Python will raise a ValueError. For example, int("hello") will fail. Always handle such cases with a try-except block or validate the input first.
# Safe conversion with try-except
try:
repeat = int(input("Enter a number: "))
print("Python" * repeat)
except ValueError:
print("Please enter a valid number.")
Debugging Tips
When you see this error, follow these steps to debug:
- Look at the line number in the error message.
- Identify which variable is being multiplied.
- Print the type of that variable using
print(type(variable)). - Check if the other operand is a string instead of an integer.
- Convert the string to an integer using
int()orfloat().
For more complex debugging, you can use Python's built-in pdb module. But for this error, the solution is usually straightforward.
Real-World Scenario: Repeating a String
Imagine you are building a program that prints a banner. The user enters the banner text and how many times to repeat it. If you forget to convert the repeat count to an integer, you will get this error.
# Banner program with error
banner = input("Banner text: ")
repeat = input("Repeat count: ")
print(banner * repeat) # Error
The fix is to convert repeat to an integer. This is a pattern you will see often in beginner projects.
Conclusion
The TypeError: can't multiply sequence by non-int of type 'str' is a common mistake. It happens when you try to multiply a string, list, or tuple by another string. The solution is always to convert the string multiplier to an integer using int() or float().
Remember these key points:
- Python sequences can only be multiplied by integers.
- User input is always a string, so convert it.
- Use
type()to check variable types during debugging. - Handle conversion errors with
try-exceptfor robust code.
By understanding this error, you will become a better Python programmer. For more help with type errors, read our article on Python TypeError: Causes and Fixes. Keep coding and learning!