| 123456789101112131415161718192021222324252627282930313233343536 |
- import os
- def isPalindrome(string):
- "Check if a string is a palindrome"
- # Get length of string
- stringLength = len(string)
- # Variable holding the new reversed string
- newString = ""
- # Position of first letter is considered 0 in string
- # Set i to position of last letter in string
- i = stringLength - 1
- # Iterate the string backwards (count backwards from length of string down to zero)
- while i >= 0:
- # Add character of position i to new reversed string
- newString = newString + string[i]
- # Decrease counter by 1
- i -= 1
- # Compare new reversed string with original string, and return result
- # Make all letters lower case to enable a correct comparison
- return string.lower() == newString.lower()
- # Clear screen
- os.system("clear") # Mac or Linux
- #os.system("cls") # Windows
- userInput = input("Input a string: ")
- # Call isPalindrome function and print output based of result
- if isPalindrome(userInput):
- print(userInput, "is a palindrome.")
- else:
- print(userInput, "is not a palindrome.")
|