Sunday 18 September 2022

Python Example Program : Palindrome

 Checking whether given word or sequence of number is palindrome

Example 1

word = input("Enter a word to check its palindrome: ")
word=word.lower()
y=len(word)
z=y-1
i=0
while (i<y):
   
    if(word[i]!=word[z]):
        print(f"{word} is not a palindrome")
        break
    i=i+1
    z=z-1    
else:
   
    print(f"{word} is a palindrome")

Output

Enter a word to check its palindrome: Madam
madam is a palindrome

----------------------------------------------------------------------------------

Example 2 : Finding Palindrome in reversing the string

a="mom"
print(f"a= {a}")
b=a[::-1] #it will reverse
print(f"After reversing, b= {b}")
if a==b:
    print(f"{a} is a palindrome")
else:
    print(f"{a} is not a palindrome")

Output

a= mom After reversing, b= mom mom is a palindrome

No comments:

Post a Comment