Blog
Checking Palindrome using if-else
- Akhila27
- October 24, 2020
- 4:57 pm
- 2 Comments
A code to check the Palindrome of a word/number using if-else statements
Welcome to the code! Here’s a simple code to check for whether the given word/number is a palindrome or not.
A palindrome is any number or word which when reversed remains the same. For example, 12121 is a palindrome number. Similarly, “LEVEL” is a palindrome.
In this code, the user is asked for a word/number and it is checked with the rest of the code will output whether the give word/number is a palindrome or not.
Note: Loved this Post? You too can publish your article on Python for fun which will be loved by millions. Contribute an Article!
a=input("Enter a string: ")#The input is either a word or a number
n=len(a)#len is used to find the length of the string
s=a.upper()
cnt=0
vc=0
for i in range(n):
if(s[i]==s[n-1-i]):#condition for verifying the first and last character are same and so on
cnt=cnt+1#the variables are to ensure that all the characters must satisfy the condition. Else it is not a palindrome
else:
vc=vc+1
if(vc!=0):#for printing the output, vc must be zero if the number of word is a palindrome
print("The string is not a palindrome.")
else:
print("The string is a palindrome")
I think we just need 3 lines for “for loop” 🙂
String Slicing might help