TypeError: 'str' object is not callable
TypeError: 'str' object is not callable in Python means something you are calling like a function is a string. Almost always a variable called str, input or format has replaced the built-in function with the same name.
How do I fix TypeError: 'str' object is not callable?
TypeError: 'str' object is not callable means the name before the brackets now holds a string. Usually you assigned text to a built-in name such as str or input, for example input = input("Name: "). Rename that variable to something like name or text, and the built-in function works again.
- 1. Name before the bracketPrint type() of the name right before ( on the failing line. If it says str, that name was reassigned.
- 2. Variables named like built-insSearch the file and function parameters for str =, input =, format = or def f(str). Rename them.
- 3. A missing + or comma"Total: "(total) calls a string. Write "Total: " + str(total), or add the missing comma in a list or print().
What does TypeError: 'str' object is not callable mean?
In Python, name(...) means call whatever name refers to right now. Built-in names such as str, input, sum and max are ordinary variables, not reserved words: assign a string to one and every later call to it in that scope tries to call the string.
Why does TypeError: 'str' object is not callable happen?
1. A variable or parameter named str
Inside the function, str is now the text you passed in, so str(n) calls a string.
Breaks
def label(str, n):
return str + " #" + str(n)Works
def label(text, n):
return text + " #" + str(n)2. Storing input() in a variable called input
The second call to input() calls the answer to the first one.
Breaks
input = input("Name: ")
age = input("Age: ")Works
name = input("Name: ")
age = input("Age: ")3. A string followed by brackets
A missing + or comma turns the brackets into a call.
Breaks
message = "Total: "(total)Works
message = "Total: " + str(total)How to find it in your own code
Look at the name directly before the ( on the failing line and print(type(name)). If it says <class 'str'>, search upwards for an assignment to that name and rename the variable. Never reuse str, list, dict, input, sum, max, min, type or id as a variable name.
Try it: fix this bug
mediummake_label("Order", 5) crashes: TypeError: 'str' object is not callable.
def make_label(str, number):
return str + " #" + str(number)Still not sure why yours breaks?
Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.