a=raw_input("enter the word")
b=[str(letter) for letter in a]
c=b[::-1]
print c
if b==c:
print ("it is a palindrome")
else:
print "it is not a palindrome"
this program works fine..just one question how do i modify it so that it prints nurses run as a palindrome. it is currently printing nursesrun as a palindrome. please help and suggest. this code is for python 2.7 and please to improvise on this code only. thanks.
While there are better ways of doing this, this example shows an improvement (specifically) for your code. you can check for letter.isalpha()
b = [str(letter) for letter in a if letter.isalpha()]
and now the check would work.
Demo:
>>> x = "nurses run"
>>> b=[str(letter) for letter in x if letter.isalpha()]
>>>
>>> b
['n', 'u', 'r', 's', 'e', 's', 'r', 'u', 'n']
>>> c = b[::-1]
>>> b == c
True
>>>
EDIT:
The code using replace
:
x = x.replace(" ", "")
>>> x = "nurses run"
>>> x = x.replace(" ", "")
>>> x
'nursesrun'
Note that you would have to replace explicitly every single type this way.