Close
Close

How to store a string value when returned by a user defined function in python?

   Kartikey_Chauhan

import re
def palindrome(st1,st2,i):
    if(i<0):
        print(st2)
        return st2       
    else:
        st2=st2+st1[i]
        i=i-1
        palindrome(st1,st2,i)
def main():
    st0=input("enter the string")
    st=""
    le=len(st0)-1
    stk=palindrome(st0,st,le)
    print(stk)

    if(bool(re.match(st0,stk))):
        print("string is palindrome")
    else:
        print("string is not palindrome")
main()       

 

 

 

I am running the above code and from function “palindrome” a string value is returned  and stored in variable “stk” in side main.

But when I print ‘stk’ then the value printed is none .

Where am Iwrong ? Please tell me.


Answers

  •   

    How to store a string value when returned by a user-defined function in python?

    Ans- Just the way you did.

    stk=palindrome(st0,st,le)
    

    Why there is a none in the output?

    Because you defined the string to do so. None is NULL of python. Also, "None" refers exactly to the intended functionality - it is nothing, and has no behaviour. When you declared

    st = “”
    

    You made string st to store none. Now when you concatenate your sting with none, it becomes like none + reversed st0. That is why you are getting a None printed in the output. That is also the reason for the wrong result for an input string which is a palindrome.



Ask Yours
Post Yours
Write your answer