OneCompiler

palindromeCheck.py

278
# check letters from edged to the center
# without checking the number of the letters in word (odd or even)
word = input("Enter the word to check: ")
left = 0
right = len(word)-1
isPalindrome = True
while left <= right:
    if word[left] != word[right]:
        isPalindrome = False
        break
    left += 1
    right -= 1
if isPalindrome:
    print(f"The {word} is a palindrome")
else:
    print(f"The {word} is not a palindrome")


# check letters from center to the edges
# without checking the number of the letters in word (odd or even)
length = len(word)
mid = length//2
isPalindrome = True
i = 0
while i < mid:
    if word[mid - 1 - i] != word[-(mid - i)]:
        isPalindrome = False
        break
    i += 1

if isPalindrome:
    print(f"The {word} is a palindrome")
else:
    print(f"The {word} is not a palindrome")