Why is the print statement printing None at the end

Question:

def empty(new):
    new1=''
    for i in new:
        new1=i+new1
        print(new1)
    print(new1)    

new='First question'
print(new)
print(empty(new))

output:

First question
F
iF
riF
sriF
tsriF
 tsriF
q tsriF
uq tsriF
euq tsriF
seuq tsriF
tseuq tsriF
itseuq tsriF
oitseuq tsriF
noitseuq tsriF
noitseuq tsriF
None

Question:
why do i get none at the end???

Asked By: Prakash101

||

Answers:

I think what you want is to replace print(empty(new)) with empty(new). In the first case you are printing the return value of the call to the function empty, which in this case is None because the function doesn’t have a return statement.

Answered By: Constantine32

You can either add a return to the function:

def empty(new):
    new1=''
    for i in new:
        new1=i+new1
        print(new1)
    return new1   

new='First question'
print(new)
print(empty(new))

Or call function not in print:

def empty(new):
    new1=''
    for i in new:
        new1=i+new1
        print(new1)
    print(new1)  

new='First question'
print(new)
empty(new)
Answered By: Rarblack
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.