TypeError: 'str' object is not callable in Python 3

Dung Do Tien Apr 22 2022 208

Hello Guys, I'm a beginner in Python and started learning it yesterday. I tried to write something basic with a function and learn how to output data. I created a function to help return the sum of two numbers and use the print() method to help print out the value. You can see here:

 import numpy as np

def sumable(a ,b) :
  return a + b
  
str = "Total your working days are: "
totalWorkingDay = sumable(10, 16)

print(str(str + str(totalWorkingDay)))

But when I run this code I got an exception TypeError: 'str' object is not callable.

 Traceback (most recent call last):
  File "main.py", line 10, in <module>
    print(str(str + str(totalWorkingDay)))
TypeError: 'str' object is not callable

I research from Google and know str() is a build-in function of Python and it helps convert other data-type to strings.

Why did I get this error? Thanks for any suggestions.

Have 1 answer(s) found.
  • R

    Raja T Apr 22 2022

    This error occurred because you have the same variable with the name str. So Python will default understand that the str() function is a variable. All you need is change your variable to another name, and not allow duplicates with any keyword in Python. For example:

     import numpy as np
    
    def sumable(a ,b) :
      return a + b
      
    msg = "Total your working days are: "
    totalWorkingDay = sumable(10, 16)
    
    print(str(msg + str(totalWorkingDay)))

    #Output

     Total your working days are: 26

    happy code :d

Leave An Answer
* NOTE: You need Login before leave an answer

* Type maximum 2000 characters.

* All comments have to wait approved before display.

* Please polite comment and respect questions and answers of others.

Popular Tips

X Close