Reputation:
Right now, what I have is a code that I can test a single word whether it is a Palindrome or not. I have to input the word, and it will tell me whether it is a Palindrome (True) or if it is not (False)
I need to create one that Asks for a single word, then provides a True of False based on the word that is typed. This is what i have so far.
I really have no idea how to do this, any help would be greatly appreciated.
def isPalindrome(s):
if len(s) <= 1:
return True
else:
if s[0] != s[len(s)-1]:
return False
else:
return isPalindrome(s[1:len(s)-1])
print(isPalindrome("poop"))
Upvotes: 2
Views: 27397
Reputation: 2445
Simply create a reversed string and check if both are equal.
def isPalindrome(s):
return s == s[::-1]
print(isPalindrome('poop'))
Upvotes: 20
Reputation: 187
For the people doing the palindrome test from testdome.com that takes into account casing, here is my solution:
def is_palindrome(word):
wordoriginal = []
wordreversed = []
for i in reversed(word):
i = i.lower()
wordreversed.append(i)
for i in word:
i = i.lower()
wordoriginal.append(i)
return wordoriginal == wordreversed
Upvotes: 0
Reputation: 39
This should work.
return (s[:1+len(s)//2]==s[len(s)//2:][::-1])
Upvotes: 0
Reputation: 1458
Using string subscripting
def is_palindrome(string):
return all(char == string[-i - 1] for i, char in enumerate(string))
Using list reversing
def is_palindrome(string):
to_list = list(string)
# reverse to_list now
to_list.reverse()
# our reversed list should be equal string as a list
# if string is a palindrome
return to_list == list(string)
Upvotes: 1
Reputation: 13
This is mine:
def palindrome_checker(word):
return True if len(word) < 2 else (word[0] == word[-1]) and palindrome_checker(word[1:-1])
Upvotes: 0
Reputation: 1
This is what I came up with, hope it works for you:
def palindrome_(word):
word = input("enter your word Here ")
return word == word[::-1]
print palindrome_("word")
Upvotes: -1
Reputation: 21991
You might try this function:
def is_palindrome(text):
return text[:len(text)//2] == text[:(len(text)-1)//2:-1]
Here is example usage for reference:
>>> is_palindrome('')
True
>>> is_palindrome('a')
True
>>> is_palindrome('b')
True
>>> is_palindrome('aa')
True
>>> is_palindrome('ab')
False
>>> is_palindrome('ba')
False
>>> is_palindrome('bb')
True
>>> is_palindrome('aaa')
True
>>> is_palindrome('aab')
False
>>> is_palindrome('aba')
True
Upvotes: 2
Reputation: 11
here is mine:
def isPalindrome(word):
word=str(word)
a=word[::-1]
if a!=word:
return False
elif word=='':
return False
else: return True
Upvotes: 1
Reputation: 1858
If you are asking how to get Python to take user input, then there are a couple options available to you. One option is to make your script run from the command line and use a command line argument and the other is to use the raw_input function. Both are implemented in the following code.
import sys
def isPalindrome(word):
...
if __name__ == '__main__':
if len(sys.argv) > 1:
inp = sys.argv[1]
else:
inp = raw_input("Type a word: ") # Which you might want to strip for white space
if isPalindrome(inp):
print inp,"is a palindrome"
else:
print inp,"is not a palindrome"
Several other people have suggested alternative implementations for isPalindrome that are probably better, but if you are doing this as an assignment and are supposed to use recursion, then keep using yours. Also, the raw_input function can be called anywhere that is convenient in your script and doesn't have to be used when called from the command line.
Upvotes: 2
Reputation: 3417
Also using a reversed string, but can be used in-line, as well (i.e., doesn't require a function).
def is_palindrome(word):
return word == ''.join(reversed(word))
print is_palindrome('hello') #False
print is_palindrome('racecar') #True
Upvotes: 2