How To Fix Python Error Typeerror: ‘str’ Object Is Not Callable

When i develop python source code, i want to use python built-in function str() to conver a number variable to string type. But i meet below error Typeerror: ‘str’ Object Is Not Callable. 

>>> x = 1.99
>>>
>>> str(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

This error confused me a lot for some time. But i finally fix it after google it. The reason for this error is because i had use a variable name str in the same python interpreter, and i had assign string value to that variable str. So the python interpreter treat str as a variable name other than built-in function name str(). So it throw out TypeError: ‘str’ object is not callable. Below is the full python source  code.

>>> global str
>>>
# some times ago, i assign string 'hello' to str variable, so python interpreter think str is a string variable.
>>> str = 'hello'
>>>
......
......
......
>>> x= 1.99
# so when i invoke str() function, the interpreter think str is a string variable then error happened.
>>> str(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'str' object is not callable

Conclusion : Do not declare python variable name same with python built-in function name, keyword etc.

3 thoughts on “How To Fix Python Error Typeerror: ‘str’ Object Is Not Callable”

  1. Hello, I do not even have any str variables and it still gives me Type error.

    My code:

    haracters = [‘a”b”c”d”e”f”g”h”i”j”k”l”m”n”o”p”q”r”t”u”v”w”x”y”z’]
    numbers = [‘1”2”3”4”5”6”7”8”9”10”11”12”13”14”15”16”17”18”19”20”21”22”23”24’]
    input = input(‘Write Text: ‘)
    input = input.lower()
    output = []
    for character in input:
    number = ord(character) – 96
    output.append(number)
    print(output)

    MAX = 100

    # Function to print Matrix

    def printMatrix(M, rowSize, colSize):

    global row1
    for i in range(rowSize):

    for j in range(colSize):

    print(M[i][j], end = ” “)

    print()
    # Function to multiply two matrices

    # A[][] and B[][]

    def multiplyMatrix(row1, col1, A,

    row2, col2, B):

    # Matrix to store the result

    C = [[0 for i in range(MAX)]

    for j in range(MAX)]

    # Check if multiplication is Possible

    if (row2 != col1):

    print(“Not Possible”)

    return

    # Multiply the two

    for i in range(row1):

    for y in range(col2):

    C[i][j] = 0

    for k in range(row2):

    C[i][j] += A[i][k] * B[k][j]

    # Print the result

    print(“Resultant Matrix: “)

    printMatrix(C, row1, col2)

    # Driver Code

    if __name__ == “__main__” :

    A = [[0 for i in range(MAX)]

    for j in range(MAX)]

    B = [[0 for i in range(MAX)]

    for j in range(MAX)]

    # Read size of Matrix A from user

    row1 = int(input(‘Enter the number of rows of First Matrix: ‘))

    col1 = int(input(“Enter the number of columns of First Matrix: “))

    # Read the elements of Matrix A from user

    print(“Enter the elements of First Matrix: “)

    for i in range(row1):

    for j in range(col1) :

    A[i][j] = int(input(“A[” + str(i) +

    “][” + str(j) + “]: “))

    # Read size of Matrix B from user

    row2 = int(input(“Enter the number of rows of Second Matrix: “))

    col2 = int(input(“Enter the number of columns of Second Matrix: “))

    # Read the elements of Matrix B from user

    print(“Enter the elements of Second Matrix: “);

    for i in range(row2) :

    for j in range(col2) :

    B[i][j] = int(input(“B[” + str(i) +

    “][” + str(j) + “]: “))

    # Print the Matrix A

    print(“First Matrix: “)

    printMatrix(A, row1, col1)

    # Print the Matrix B

    print(“Second Matrix: “)

    printMatrix(B, row2, col2)

    # Find the product of the 2 matrices

    multiplyMatrix(row1, col1, A, row2, col2, B)

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.