7.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536
  1. import os
  2. def isPalindrome(string):
  3. "Check if a string is a palindrome"
  4. # Get length of string
  5. stringLength = len(string)
  6. # Variable holding the new reversed string
  7. newString = ""
  8. # Position of first letter is considered 0 in string
  9. # Set i to position of last letter in string
  10. i = stringLength - 1
  11. # Iterate the string backwards (count backwards from length of string down to zero)
  12. while i >= 0:
  13. # Add character of position i to new reversed string
  14. newString = newString + string[i]
  15. # Decrease counter by 1
  16. i -= 1
  17. # Compare new reversed string with original string, and return result
  18. # Make all letters lower case to enable a correct comparison
  19. return string.lower() == newString.lower()
  20. # Clear screen
  21. os.system("clear") # Mac or Linux
  22. #os.system("cls") # Windows
  23. userInput = input("Input a string: ")
  24. # Call isPalindrome function and print output based of result
  25. if isPalindrome(userInput):
  26. print(userInput, "is a palindrome.")
  27. else:
  28. print(userInput, "is not a palindrome.")