Skip to content

Instantly share code, notes, and snippets.

@MohanSha
Created February 13, 2018 08:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MohanSha/695ce1c1c69fccdc747a8cf02a1aa22d to your computer and use it in GitHub Desktop.
Save MohanSha/695ce1c1c69fccdc747a8cf02a1aa22d to your computer and use it in GitHub Desktop.
Checks if the string entered by the user is a palindrome. That is that it reads the same forwards as backwards like “racecar”
# Check if Palindrome
# Checks if the string entered by the user is a palindrome.
# That is that it reads the same forwards as backwards like “racecar”
def is_palindrome(string):
if len(string) <= 1:
return True
if string[0] != string[-1]:
return False
return is_palindrome(string[1:-1])
def is_palindrome_alt(string):
return string == string[::-1]
string = raw_input('String: ').lower()
if is_palindrome_alt(string):
print string + ' is a palindrome'
else:
print string + ' is not a palindrome'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment