forked from kHarshit/python-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpalindrome.py
More file actions
34 lines (28 loc) · 883 Bytes
/
palindrome.py
File metadata and controls
34 lines (28 loc) · 883 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def is_word_palindrome(word):
"""Checks if a word is palindrome."""
word = word.lower()
word_rev = word[::-1] # the reverse property of list indexing works on strings too
if word == word_rev:
return True
else:
return False
def is_palindrome(s):
"""Checks if a string is a palindrome."""
def to_chars(s):
"""removes whitespaces from the string"""
s = s.lower()
ans = ""
for c in s:
if c in 'abcdefghijklmnopqrstuvwxyz':
ans = ans + c
return ans
# def is_pal(s):
# if len(s) <= 1:
# return True
# else:
# return s[0] == s[-1] and is_pal(s[1:-1])
#
# return is_pal(to_chars(s))
return is_word_palindrome(to_chars(s))
inp = input("Enter a word/sentence to check if it is palindrome: ")
print(is_palindrome(inp))