In Python, the TypeError: can't multiply sequence by non-int of type 'str'
error occurs when you try to multiply a sequence type (for example, string, list, tuple, etc.) with a string type value. This error can be frustrating, especially if you are new to Python programming. In this tutorial, we’ll dive deep into the common scenarios in which this error can error and how to fix it.

Understanding the TypeError: can't multiply sequence by non-int of type 'str'
error
In Python, a sequence is a collection of ordered and indexed elements. The most common sequence types in Python are lists, tuples, and strings. Python allows you to multiply sequences by integer values to create a new sequence that repeats the original sequence.
Let’s look at an example.
# create a string s = "hello" print(s * 3)
Output:
hellohellohello
In the above example, we multiply the string s
with the integer 3. You can see that the resulting value is the string s
repeated 3 times.
You can similarly multiply other sequence types such as lists and tuples with integer values.
# create a list ls = [1, 2, 3] print(ls * 3)
Output:
[1, 2, 3, 1, 2, 3, 1, 2, 3]
# create a tuple tup = ('cat', 'dog', 'bunny') print(tup * 2)
Output:
('cat', 'dog', 'bunny', 'cat', 'dog', 'bunny')
Reproducing the error
In the above examples, we saw that we can multiply sequences with integer values to get repeated sequences. But, if you multiply a sequence with a string value (which doesn’t really make sense but if you do), you’ll get an error.
Let’s look at an example.
# create a string s = "hello" # multiply string with a string value print(s * "cat")
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[4], line 4 2 s = "hello" 3 # multiply string with a string value ----> 4 print(s * "cat") TypeError: can't multiply sequence by non-int of type 'str'
Here, we multiplied the s
with the string value “cat”, on running the code we get the error, TypeError: can't multiply sequence by non-int of type 'str'
.
You’ll get this error on multiplying other sequence types with a string value as well.
# create a list ls = [1, 2, 3] # multiply list with a string value print(ls * "abc")
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[5], line 4 2 ls = [1, 2, 3] 3 # multiply list with a string value ----> 4 print(ls * "abc") TypeError: can't multiply sequence by non-int of type 'str'
# create a tuple tup = ('cat', 'dog', 'bunny') # multiply tuple with a string value print(tup * "parrot")
Output:
--------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[6], line 4 2 tup = ('cat', 'dog', 'bunny') 3 # multiply tuple with a string value ----> 4 print(tup * "parrot") TypeError: can't multiply sequence by non-int of type 'str'
Why does this error occur?
In the above examples, we saw that multiplying a sequence type in Python with a string value will result in a TypeError: can't multiply sequence by non-int of type 'str'
. This is because you can only multiply a sequence type with an integer and not any other type.
A common scenario in which this error occurs is when you’re taking input from the user, for example, if you’re using the input()
method. The returend value from the input()
is a string even if the user enters a numerical value. So, if you try to multiply a sequence with this value, you’ll get the above error.
Let’s look at an example.
# create a string message = "hello" # ask user for the number of times to repeat the message n = input("How many times to repeat the message?") # display the repeated message print(message * n)
Output:
How many times to repeat the message?3 --------------------------------------------------------------------------- TypeError Traceback (most recent call last) Cell In[8], line 6 4 n = input("How many times to repeat the message?") 5 # display the repeated message ----> 6 print(message * n) TypeError: can't multiply sequence by non-int of type 'str'
In the above example, we gave 3 as the input but since the input()
method reads the values as string, the value stored in the variable n
is of string type.
Fixing the error
To fix this error, instead of multiplying the sequence with a string type value, multiply it with an integer type value. Keep in mind that the purpose of multiplying a sequence with an integer is to get a repeated sequence.
Let’s take the case of the using the input()
method where we’re asking the user to input the number of times to repeat the message. To fix the error, we’ll convert the input value to an integer using the int()
method and then use that value in the multiplication.
# create a string message = "hello" # ask user for the number of times to repeat the message n = int(input("How many times to repeat the message?")) # display the repeated message print(message * n)
Output:
How many times to repeat the message?3 hellohellohello
You can see that we don’t get that error and our message is repeated n
number of times.
Conclusion
The TypeError: can't multiply sequence by non-int of type 'srt'
error occurs when you try to multiply a sequence type in Python (for example, list, tuple, string) with a string value. You can only multiply sequences with integer values in Python which will result in repeated sequences. Thus, to fix this error, you need to multiply the sequence with an integer value if you want a repeated sequence.
You might also be interested in –