In Python, the TypeError: can not increase series by non-int of kind 'str'
mistake happens when you attempt to increase a series kind (as an example, string, listing, tuple, and so on) with a string kind worth. This mistake can be irritating, specifically if you are brand-new to Python programs. In this tutorial, we’ll dive deep right into the typical situations in which this mistake can mistake as well as exactly how to repair it.


Recognizing the TypeError: can not increase series by non-int of kind 'str'
error
In Python, a series is a collection of purchased as well as indexed components. One of the most typical series key ins Python are checklists, tuples, as well as strings. Python enables you to increase series by integer worths to develop a brand-new series that duplicates the initial series.
Let’s consider an instance.
# develop a string s="hello there". print( s * 3)
Output:
hellohellohello
In the above instance, we increase the string s
with the integer 3. You can see that the resulting worth is the string s
duplicated 3 times.
You can likewise increase various other series kinds such as checklists as well as tuples with integer worths.
# develop a checklist. ls = [1, 2, 3]. print( ls * 3)
Output:
[1, 2, 3, 1, 2, 3, 1, 2, 3]
# develop a tuple. tup = (' feline', 'pet dog', 'rabbit'). print( tup * 2)
Output:
(' feline', 'pet dog', 'rabbit', 'feline', 'pet dog', 'rabbit')
Reproducing the error
In the above instances, we saw that we can increase series with integer worths to obtain duplicated series. Yet, if you increase a series with a string worth (which does not truly make good sense however if you do), you’ll obtain a mistake.
Let’s consider an instance.
# develop a string. s="hello there". # increase string with a string worth. print( s * "feline")
Output:
---------------------------------------------------------------------------. TypeError Traceback (latest phone call last). Cell In[4], line 4. 2 s="hello there". 3 # increase string with a string worth. -- > 4 print( s * "feline"). TypeError: can not increase series by non-int of kind 'str'
Here, we increased the s
with the string worth “feline”, on running the code we obtain the mistake, TypeError: can not increase series by non-int of kind 'str'
.
You’ll obtain this mistake on increasing various other series kinds with a string worth too.
# develop a checklist. ls = [1, 2, 3]. # increase listing with a string worth. print( ls * "abc")
Output:
---------------------------------------------------------------------------. TypeError Traceback (latest phone call last). Cell In[5], line 4. 2 ls = [1, 2, 3]. 3 # increase listing with a string worth. -- > 4 print( ls * "abc"). TypeError: can not increase series by non-int of kind 'str'
# develop a tuple. tup = (' feline', 'pet dog', 'rabbit'). # increase tuple with a string worth. print( tup * "parrot")
Output:
---------------------------------------------------------------------------. TypeError Traceback (latest phone call last). Cell In[6], line 4. 2 tup = (' feline', 'pet dog', 'rabbit'). 3 # increase tuple with a string worth. -- > 4 print( tup * "parrot"). TypeError: can not increase series by non-int of kind 'str'
Why does this mistake happen?
In the above instances, we saw that increasing a series enter Python with a string worth will certainly cause a TypeError: can not increase series by non-int of kind 'str'
. This is since you can just increase a series kind with an integer as well as none various other kind.
A typical circumstance in which this mistake is when you’re taking input from the customer, as an example, if you’re making use of the input()
technique. The returend worth from the input()
is a string also if the customer gets in a mathematical worth. So, if you attempt to increase a series with this worth, you’ll obtain the above mistake.
Let’s consider an instance.
# develop a string. message="hello there". # ask customer for the variety of times to duplicate the message. n = input(" The number of times to duplicate the message?"). # show the duplicated message. print( message * n)
Output:
The number of times to duplicate the message? 3. ---------------------------------------------------------------------------. TypeError Traceback (latest phone call last). Cell In[8], line 6. 4 n = input(" The number of times to duplicate the message?"). 5 # show the duplicated message. -- > 6 print( message * n). TypeError: can not increase series by non-int of kind 'str'
In the above instance, we offered 3 as the input however because the input()
technique checks out the worths as string, the worth kept in the variable n
is of string kind.
Taking care of the error
To repair this mistake, rather than increasing the series with a string kind worth, increase it with an integer kind worth. Bear in mind that the function of increasing a series with an integer is to obtain a duplicated series.
Let’s take the instance of the making use of the input()
technique where we’re asking the customer to input the variety of times to duplicate the message. To repair the mistake, we’ll transform the input worth to an integer making use of the int()
technique and afterwards utilize that worth in the reproduction.
# develop a string. message="hello there". # ask customer for the variety of times to duplicate the message. n = int( input(" The number of times to duplicate the message?")). # show the duplicated message. print( message * n)
Output:
The number of times to duplicate the message? 3. hellohellohello
You can see that we do not obtain that mistake as well as our message is duplicated n
variety of times.
Conclusion
The TypeError: can not increase series by non-int of kind 'srt'
mistake happens when you attempt to increase a series enter Python (as an example, listing, tuple, string) with a string worth. You can just increase series with integer worths in Python which will certainly cause duplicated series. Therefore, to repair this mistake, you require to increase the series with an integer worth if you desire a duplicated series.
You may likewise have an interest in–