← Back

Home â€ē Errors â€ē TypeError

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

Exception: TypeErrorCategory: runtimeRaised at: the * operator, when one side is a sequence and the other is a string

What this error means

You used * between a sequence (string, list or tuple) and a string. Python gives * a second meaning for sequences — repeat — so "ab" * 3 is 'ababab'. That second meaning only accepts an integer repeat count. Two strings on either side of * satisfy neither rule, hence the deliberately wordy message.

Read it backwards: can't multiply sequence (the left side is a str, list or tuple) by non-int of type 'str' (the right side is a string, and only an int would do).

Cause 1 — input() always returns a string

This is the cause in almost every beginner report. input() hands back text, even when the user typed digits. "120" is a one-character-per-digit string, not the number 120:

price = input("Price: ")   # user types 120 -> the string '120'
qty = input("Qty: ")       # user types 3   -> the string '3'

print(price * qty)         # TypeError
Traceback (most recent call last):
  File "demo.py", line 4, in <module>
    print(price * qty)
          ~~~~~~^~~~~
TypeError: can't multiply sequence by non-int of type 'str'

Convert at the boundary, the moment the text arrives:

price = float(input("Price: "))   # 120.0
qty = int(input("Qty: "))         # 3

print(price * qty)                # 360.0

Cause 2 — data that arrived as text

The same trap appears everywhere text becomes data: a CSV field, a URL query parameter, sys.argv, an environment variable, a JSON string value, or a database column typed as text.

import csv, os

with open("scores.csv", encoding="utf-8") as f:
    for row in csv.reader(f):
        name, marks = row[0], int(row[1])     # csv gives strings

limit = int(os.environ.get("LIMIT", "10"))    # env vars are strings
total = marks * limit

Cause 3 — one side converted, the other forgotten

A half-converted expression is easy to miss because it looks almost right. Note also what does work, so the difference is clear:

"5" * 3        # '555'      — legal: repeat the string 3 times
"5" * "3"      # TypeError  — this error
5 * 3          # 15         — legal: arithmetic
[0] * 3        # [0, 0, 0]  — legal: repeat the list
[0] * "3"      # TypeError  — same error, list on the left

How to fix it

  1. Convert at the point of entry, not at the point of use: int(input()) or float(input()). Every later line then works with real numbers.
  2. Pick the right converter — int() for whole counts, float() for money and measurements. int("12.5") raises ValueError, so use float() first if the text may contain a decimal point.
  3. Wrap the conversion, not the whole program: a try/except ValueError around the int() lets you re-prompt instead of crashing.
  4. Print repr(value) while debugging — quotes mean it is still a string. type(value) says the same thing more formally.
  5. Strip stray characters before converting: int(text.strip()) tolerates the spaces and newlines that input() and file reads leave behind.
while True:
    raw = input("Qty: ").strip()
    try:
        qty = int(raw)
        break
    except ValueError:
        print(f"'{raw}' ek number nahi hai, dobara likho.")

Fix it interactively

These free problems are the same type-confusion bug — repair them in the browser:

Practice more Python bugs →

Related messages worth knowing

đŸ‡ŽđŸ‡ŗ Hindi ā¤ŽāĨ‡ā¤‚ ā¤¸ā¤Žā¤āĨ‡ā¤‚

TypeError: can't multiply sequence by non-int of type 'str' ka matlab hai aapne string ko string se multiply kiya. Wajah lagbhag hamesha yahi hoti hai ki input() hamesha string deta hai — user ne 120 type kiya, par mila '120'. Isliye price * qty me dono text hote hain. Python me * ka doosra matlab 'repeat' hota hai ("ab" * 3 = 'ababab'), aur wo sirf integer accept karta hai. Fix: conversion entry point par karo — price = float(input("Price: ")) aur qty = int(input("Qty: ")). Yaad rakho int("12.5") ValueError deta hai, isliye decimal ke liye float() use karo. Debug karte waqt repr(value) print karo — quotes dikhe to wo abhi bhi string hai.

ā¤ĒāĨ‚⤰āĨ€ Hindi explanation ā¤Ēā¤ĸā¤ŧāĨ‡ā¤‚ →