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:
Introductory ⭐
- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science
Intermediate ⭐⭐⭐
- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization
Advanced ⭐⭐⭐⭐⭐
- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science
🔎 Find Data Science Programs 👨💻 111,889 already enrolled
Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.
('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 –