How to print the docstring of the `input` function?

Write a python script to print the docstring(documentation string) of the input function in Python 3?

I’m new to Python and unable to find the answers for this question.

1 Like

Hi David, and welcome.

This sounds like a homework exercise. What have you tried?

You want to print the docstring of the input function. Here are all
the individual pieces you need to solve the problem, without giving you
the exact answer.

You want to get access to the input function without calling the
function. Just leave off the parentheses!

answer = input("question?")  # calls the function

obj = input  # doesn't call the function

You want to see the doc string of an object, if it has one:

# Note that this is *two* leading and trailing underscores
# not just one
docs = myobject.__doc__

You want to see the doc string of the builtin len function:

docs = len.__doc__  # don't call the function!

You want to print something:

print(something)

Put all the pieces together, and you will be able to print the input
function’s doc string.

If you can’t work it out, show what you tried, and what result you got.

3 Likes

Thanks for the reply. You meant to say that I have combine all the pieces to make it right?

1 Like

I tried as follows but ended up with name error:(. Could you please correct me?

answer = input(“question?”) # calls the function
obj = input # doesn’t call the function
docs = obj.doc
docs = len.doc
print(something)

ERROR:

python3 app.py | tee output.txt
question?python3 app.py | tee output.txt
Traceback (most recent call last):
File “app.py”, line 3, in
docs = myobject.doc
NameError: name ‘myobject’ is not defined
vim app.py python3 app.py | tee output.txt
question?

I think there are two options:

  1. just use print(input.doc)
  2. or use help(input)

Anyone should work.

u can try in this way
def input():
‘’‘this is doc string’’’
return None
print(input.doc)
help(input)
you got your ans. this code work 100%

You can use below.

def input():
‘’‘Hello’’’
print(help(input)) or help(input)

i have error en ““this is doc string””
you help me!

It should be either one set of quotes or three, but not two:

# Okay
"this is a one-line doc string"

# better
"""this is a doc string,
It can have more than one line.
"""

# Invalid syntax.
"" two quotes doesn't work ""