Skip to content
  • YouTube
  • FaceBook
  • Twitter
  • Instagram

Data Analytics Ireland

Data Analytics and Video Tutorials

  • Home
  • Contact
  • About Us
    • Latest
    • Write for us
    • Learn more information about our website
  • Useful Links
  • Glossary
  • All Categories
  • Faq
  • Livestream
  • Toggle search form
  • raise an exception in python class
  • Recursion Definition
  • TypeError: cannot unpack non-iterable int object Python
  • How can packages be managed in Julia? Julia programming
  • What is a CTE in SQL? SQL
  • Python Tutorial: How to import data from files Python working with files
  • How to run Python directly from Javascript Flask
  • How to data cleanse a database table Python Data Cleansing

TypeError: ‘str’ object is not callable

Posted on February 15, 2021February 23, 2023 By admin

Estimated reading time: 2 minutes

This is a common TypeError that you will come across in Python, it is actually easy to diagnose how it occurred.

To start off we need to understand “str” is a function in python, and it converts any value into a string.

As a result, because it is a function your ability to call it has limitations.

So in essence it has parenthesis () beside it, and allows parameters to be passed to it.

So let’s first look at how the string function works:

x = str("10")
y = 10
print(type(x))
print(type(y))
print(x)
print(y)
With output:
<class 'str'>
<class 'int'>
10
10

As you will see above the value 10, on its own is an integer, but when you call the string function, it now becomes a string.

For this reason, this, calling a string function, completes a conversion to a string of ten, but what if the variable is called str?

Let’s take an example below from an input:

str = input("what year where you born?")
print(str(str))
Output:
what year where you born?2021
Traceback (most recent call last):
  File "str obj is not callable.py", line 2, in &lt;module>
    print(str(str))
TypeError: 'str' object is not callable
Process finished with exit code 1

The reason for this error above is that we have named the variable “str”.

As can be seen, the program is trying to use the first str in the print statement as a function.

As we know by now string variables are not callable.

Accordingly, the function str() which the program is trying to run, fails with the TypeError identified.

For this reason, to fix this problem we would change the variable called str to “year”, the error then disappears.

The updated code will work as when calling str(), it is not conflicted with a variable name.

year = input("what year where you born?")
print(str(year))
Output:
what year where you born?2021
2021
Process finished with exit code 0

So to summarise:

  • Strings are not callable
  • Don’t name a variable as str, or any function name, these are reserved words by the system.
  • Functions are callable, strings are not.

Python Functions, type error Tags:object is not callable, python objects, str object is not callable, string object, TypeError

Post navigation

Previous Post: TypeError: cannot unpack non-iterable int object
Next Post: How To Join Tables In SQL

Related Posts

  • How to pass by reference or pass by value in Python Python
  • TypeError: the first argument must be callable Python
  • TypeError: ‘float’ object is not callable Python
  • Create a HTML Table From Python Using Javascript Javascript
  • What are Local and Global Variables in Python? Python
  • How to Add Formulas to Excel using Python numpy

Select your language!

  • हिंदी
  • Español
  • Português
  • Français
  • Italiano
  • Deutsch
  • What is Denormalization of data in a database? Working with databases
  • What Are Constraints in SQL? SQL
  • Python tutorial: Create an input box in Tkinter Python
  • What are the built-in exceptions in Julia? Julia programming
  • What does a data analyst do? Livestream
  • What is a CTE in SQL? SQL
  • What is data analytics and why it is important Articles
  • How to change the headers on a CSV file CSV

Copyright © 2023 Data Analytics Ireland.

Powered by PressBook Premium theme

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Cookie settingsACCEPT
Privacy & Cookies Policy

Privacy Overview

This website uses cookies to improve your experience while you navigate through the website. Out of these cookies, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may have an effect on your browsing experience.
Necessary
Always Enabled
Necessary cookies are absolutely essential for the website to function properly. This category only includes cookies that ensures basic functionalities and security features of the website. These cookies do not store any personal information.
Non-necessary
Any cookies that may not be particularly necessary for the website to function and is used specifically to collect user personal data via analytics, ads, other embedded contents are termed as non-necessary cookies. It is mandatory to procure user consent prior to running these cookies on your website.
SAVE & ACCEPT