TypeError: can’t multiply sequence by non-int of type ‘str’

In Python, The Error TypeError: can’t multiply sequence by non-int of type ‘str’ is due to the multiplication operator is used between two string variables. The multiply sequence is created between a string and a integer value in python. If the multiply sequence is created between two string objects, the python interpreter will throw this “TypeError: can’t multiply sequence by non-int of type ‘str'” error.

In python, if the multiplication operator is used between a string and a number, the python interpreter repeats the string for ‘n’ times. The number must be an integer. If the multiplication operator is used between two strings, the python interpreter can not repeat the string and throws the type error.

The Error “TypeError: can’t multiply sequence by non-int of type ‘str'” is because the repeat count is not available while multiplying the string in python.



Different Variation of the error

Here you can see the different variations of the error. The solution to address this issue will be added to the later part of this post.

TypeError: can't multiply sequence by non-int of type 'str'
TypeError: can't multiply sequence by non-int of type 'float'
TypeError: can't multiply sequence by non-int of type 'list'
TypeError: can't multiply sequence by non-int of type 'tuple'
TypeError: can't multiply sequence by non-int of type 'NoneType'


Exception

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print x * y
TypeError: can't multiply sequence by non-int of type 'str'
[Finished in 0.1s with exit code 1]


Root Cause

The multiplication operator is used for repeating the string ‘n’ times between a string and a number. If the multiplication operator is used between two strings, the python interpreter treats a string as a repeated string and considers an integer to be a another one. As a result, the python interpreter throws a type error saying that there is a non-integer instead of an integer.



TypeError: can’t multiply sequence by non-int of type ‘str’

The error “TypeError: can’t multiply sequence by non-int of type ‘str'” is due to the multiplication operator ( * ) is used between two string variables. If one of the string is changed to an integer number, This issue will be resolved.

In the example below, the variable x and y contains strings. The multiplication is not possible between these two string variables. The value in x is changed to an integer number. Now the string in y is repeated for x times in the output. Changing a string to a integer value will resolve this error “TypeError: can’t multiply sequence by non-int of type ‘str'”

Program

x = "5"
y = "Yawin Tutor "
print x * y

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print x * y
TypeError: can't multiply sequence by non-int of type 'str'
[Finished in 0.1s with exit code 1]

Solution

x = 5
y = "Yawin Tutor "
print x * y

Output

Yawin Tutor Yawin Tutor Yawin Tutor Yawin Tutor Yawin Tutor 
[Finished in 0.1s]


TypeError: can’t multiply sequence by non-int of type ‘float’

The error “TypeError: can’t multiply sequence by non-int of type ‘float'” is due to the multiplication operator ( * ) is used between a string and a float number. If the float number is changed to an integer number, This issue will be resolved.

Program

x = 5.0
y = "Yawin Tutor "
print x * y

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print x * y
TypeError: can't multiply sequence by non-int of type 'float'
[Finished in 0.0s with exit code 1]

Solution

x = 5
y = "Yawin Tutor "
print x * y

Output

Yawin Tutor Yawin Tutor Yawin Tutor Yawin Tutor Yawin Tutor 
[Finished in 0.1s]


TypeError: can’t multiply sequence by non-int of type ‘list’

The error “TypeError: can’t multiply sequence by non-int of type ‘list'” is due to the multiplication operator ( * ) is used between a string and a list of objects to repeat a list of objects for ‘n’ times. If the string is changed to an integer number, This issue will be resolved.

Program

x = "5"
y = [1,2,3]
print x * y

Output

Traceback (most recent call last):
  File "/Users/python/Desktop/test.py", line 3, in <module>
    print x * y
TypeError: can't multiply sequence by non-int of type 'list'
[Finished in 0.0s with exit code 1]

Solution

x = 5
y = [1,2,3]
print x * y

Output

[1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3]
[Finished in 0.1s]


TypeError: can’t multiply sequence by non-int of type ‘tuple’

The error “TypeError: can’t multiply sequence by non-int of type ‘tuple'” is due to the multiplication operator ( * ) is used between the objects as tuples. If the code has been changed to individual statements, this issue will be resolved.

Program

x = 1.0,
y = 2.3,
z = 3.4,
"output is " + str(x*y*z)

Output

Traceback (most recent call last):
  File "/Users/knatarajan2/Desktop/test.py", line 4, in <module>
    "output is " + str(x*y*z)
TypeError: can't multiply sequence by non-int of type 'tuple'
[Finished in 0.1s with exit code 1]

Solution

x = 1.0
y = 2.3
z = 3.4
print "output is " + str(x*y*z)

Output

output is 7.82
[Finished in 0.1s]


TypeError: can’t multiply sequence by non-int of type ‘NoneType’

The error “TypeError: can’t multiply sequence by non-int of type ‘NoneType'” is due to the multiplication operator ( * ) is used between the null object and the string. If the code has been changed to use the multiplication operator between an integer and a string, this issue will be resolved.

Program

x = None
y = "Yawin Tutor "
print x * y

Output

Traceback (most recent call last):
  File "/Users/knatarajan2/Desktop/test.py", line 3, in <module>
    print x * y
TypeError: can't multiply sequence by non-int of type 'NoneType'
[Finished in 0.0s with exit code 1]

Solution

x = 5
y = "Yawin Tutor "
print x * y

Output

Yawin Tutor Yawin Tutor Yawin Tutor Yawin Tutor Yawin Tutor 
[Finished in 0.0s]



Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *