Last active
October 17, 2021 20:27
-
-
Save msyvr/a51e13595583923cbf9bf1ef848bde97 to your computer and use it in GitHub Desktop.
palindrome check - compare first half and reversed second half; this works on any input (not just alphanumeric)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def pal(user_string): | |
''' | |
check if a word/phrase is a palindrome | |
''' | |
lus = list(user_string) | |
max_ind = int(len(lus)/2) | |
l1 = [lus[i] for i in range(max_ind)] | |
l2 = [lus[-(i+1)] for i in range(max_ind)] | |
if l1 == l2: | |
return True | |
else: | |
return False | |
if __name__ == "__main__": | |
user_string = input('Enter a word to see if it\'s a palindrome: ').lower() | |
if pal(user_string): | |
print(f'Yes - {user_string} is a palindrome.') | |
else: | |
print(f'No - {user_string} is not a palindrome.') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment