26
loading...
This website collects cookies to deliver better user experience
str()
which converts a specified value into a string. The str()
method takes an object as an argument and converts it into a string.str()
is a predefined function and a built-in reserved keyword in Python, you cannot use it in declaring it as a variable name or a function name. If you do so, Python will throw a typeerror: ‘ str ‘ object is not callable.str
‘ and accessing it. Let’s look at a few examples of how to reproduce the ‘str’ object is not callable error.str = "Hello, "
text = " Welcome to ItsMyCode"
print(str(str + text))
# Output
Traceback (most recent call last):
File "c:\Projects\Tryouts\listindexerror.py", line 4, in <module>
print(str(str + text))
TypeError: 'str' object is not callable
str
‘ as a variable, and we are also using the predefined str()
method to concatenate the string.str = "The cost of apple is "
x = 200
price= str(x)
print((str + price))
# output
Traceback (most recent call last):
File "c:\Projects\Tryouts\listindexerror.py", line 3, in <module>
price= str(x)
TypeError: 'str' object is not callable
str
is declared as a variable and if you str()
method to convert into a string, you will get object not callable error.text1 = "Hello, "
text2 = " Welcome to ItsMyCode"
print(str(text1 + text2))
# Output
Hello, Welcome to ItsMyCode
text = "The cost of apple is "
x = 200
price= str(x)
print((text + price))
# Output
The cost of apple is 200
%
* character in an attempt to append values during string formatting.%
to separate our string and the values we want to concatenate into our final string.print("Hello %s its %s day"("World","a beautiful"))
# Output
Traceback (most recent call last):
File "c:\Projects\Tryouts\listindexerror.py", line 1, in <module>
print("Hello %s its %s day"("World","a beautiful"))
TypeError: 'str' object is not callable
print("Hello %s its %s day"%("World","a beautiful"))
# Output
Hello World its a beautiful day
%
operator before replacing the values ("World","a beautiful")
as shown above.