0

I wrote the following program to determine whether a string s a palindrome using recursion. My problem is that I am not sure how to add a print statement that tells me whether the string is a palindrome or not. I realize that there are other codes which do the same thing, but I am trying to understand if my reasoning is correct.

import re
s= raw_input( " Enter a string to check if it is a palindrome ")
newstring = re.sub('\W', '', s)
newstring =newstring.lower()

def Palind(newstring):

    if newstring[0] != newstring[-1]:
        #print 'The given string is not a palindrome'
        return False 
    else: 
        s_remain = newstring[1:-1]
        if s_remain == '':
            return True
        elif len(s_remain) == 1 :
            return True
        else:
            return Palind(s_remain)


if Palind(newstring):
    print 'Yes'
else: 
    print 'No'  
  • possible duplicate of [How to check for palindrome using Python logic](http://stackoverflow.com/questions/17331290/how-to-check-for-palindrome-using-python-logic) – jww Mar 26 '14 at 00:48
  • 1
    Should the question's title be related to printing a variable in Python? – jww Mar 26 '14 at 00:48

2 Answers2

1

First, correctly indent your code, and properly lowercase the input:

import re
s= raw_input( " Enter a string to check if it is a palindrome ")
newstring = re.sub('\W', '', s)
newstring = newstring.lower()

def Palind(newstring):    
    if newstring[1] != newstring[-1]:
        #print 'The given string is not a palindrome'
        return False 
    else: 
        s_remain = newstring[1:-1]
        return Palind(s_remain)

Then actually call your function and deal with the result:

if Palind(newstring):
    print ('Yes')
else:
    print ('No')

That's how you print the result of your function..

You will have problems when you enter a palindrome though, because you never actually return true. You'll need to fix that by checking if you've gotten to the end of the string without returning false.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
  • You may need to add commas around the `print` to ensure it works for the Python 3 folks who land here (and who breaks basic print functionality... Can you image what would have happened if `printf` broke in C99). – jww Mar 26 '14 at 00:52
  • 1
    I'm sure Python 3 people learn that one very quickly anyway, but sure, I'll add them. – Blorgbeard Mar 26 '14 at 00:58
  • @Blorgbeard 'rodor' -> 'No'; 'a' -> IndexError: string index out of range. – TessellatingHeckler Mar 26 '14 at 01:12
  • @TessellatingHeckler yes, I'm not purporting to have fixed every issue with the code, I'm just answering "how to add a print statement". I do mention the bug you're talking about at the bottom of my answer. – Blorgbeard Mar 26 '14 at 01:36
  • @Blorgbeard I am not sure how to add the 'return True' condition in the definition of the function or to check that the function completes to the end. I assumed that if the string is not a palindrome, it will return False at some point. Is that not correct? – Shubha Rajopadhye Mar 26 '14 at 04:45
  • @ShubhaRajopadhye yes, but what will happen if the string **is** a palindrome? – Blorgbeard Mar 26 '14 at 18:59
1

Your logic is roughly right, but you are missing some things.

  1. The first character in a string is string[0] not string[1], so you are comparing the wrong characters.

  2. You need to call Palind() as well as defining it.

  3. If you correct those problems, you are taking one letter off each side of the string each time, it gets shorter and shorter - the next interesting thing that happens is you either get down to a single character or you run out of characters. You should be looking for that state.

TessellatingHeckler
  • 27,511
  • 4
  • 48
  • 87