-1

I have written a simple code in Python . I read that if we write docstring at the end of code , then it is printed as usual. My code is

a = 'good'
b=  "boy"
print(a+ b)

"""
this will be a good example
"""

The output is

goodboy

However I suppose the output should be

goodboy
this will be a good example

I am unable to understand what is wrong here. Can anyone please tell me where I have made a mistake ?

Brijesh
  • 35
  • 6
  • When you are running a code nothing will be usually printed unless you put it inside a `print` statement – Ritwik G Mar 25 '21 at 17:16
  • "I read that if we write docstring at the end of code , then it is printed as usual." — Read where? – khelwood Mar 25 '21 at 17:17

2 Answers2

0

This is just a string for python:

""" this will be a good example """

Try it like this:

a = 'good'
b=  "boy"
c = """
this will be a good example
"""
print(a + b + c)
user_na
  • 2,154
  • 1
  • 16
  • 36
0

Where did you read that? Creating a multiline string without assigning to anything is a valid statement in python, and is indeed used to create docstrings, but they are not supposed to be printed every time you run.

>>> def sample():
...     '''Sample doc'''
... 
>>> sample.__doc__
'Sample doc'

Also notice that this is useful to stub a function: instead of using pass you can document what the function is doing, which is a good practice to follow.

Pietro
  • 1,090
  • 2
  • 9
  • 15