bmsqldev
bmsqldev

Reputation: 2735

Running "print" raises "TypeError" in Python

I am using python version 3.4.3. I need to print a string. So I tried:

print ("Hello world")

I got below error

TypeError : 'str' object is not callable

I tried below way,

str  ("Hello world")

and got below output

'Hello World'

Is it possible to get the output as below?

Hello World

i.e without the quotes.

Upvotes: 0

Views: 416

Answers (2)

Chris_Rands
Chris_Rands

Reputation: 41168

You should never use variable names that shadow python built-in functions. In this case you have re-assigned print to a string:

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

Run del print to restore the functionality of the print function:

>>> print = "s"
>>> type(print)
<class 'str'>
>>> del print
>>> type(print)
<class 'builtin_function_or_method'>
>>> print("Hello world")
Hello world

Upvotes: 3

Jona
Jona

Reputation: 1288

print ("Hello world")

works as intended for me, one way to get the

TypeError: 'str' object is not callable

is for example with this :

print ("Hello world"())

You don't have redefined the 'print' function by mystake ?

Upvotes: 1

Related Questions