Skip to content

Instantly share code, notes, and snippets.

@msyvr
Last active October 17, 2021 20:27
Show Gist options
  • Save msyvr/a51e13595583923cbf9bf1ef848bde97 to your computer and use it in GitHub Desktop.
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)
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