Checking Palindrome using if-else

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")				
			
0 0 votes
Rating
Subscribe
Notify of
guest
2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Tran Tung Duong
Tran Tung Duong
2 years ago

I think we just need 3 lines for “for loop” 🙂

def is_palindrome(string):
    s  = string.lower()
    n = len(s)
    for i in range(n):
        if (s[i] != s[n - 1 - i]):
            return False
    return True


is_palindrome(input('Enter a string: '))
Last edited 2 years ago by Tran Tung Duong
Arnav
Arnav
1 year ago
str_input = input("Enter word: ")
str_input= str_input.replace(' ', '')
if str_input == str_input[::-1]:
  print("Word is a palindrome!")
else:
  print("Word is not a Palindrome!")

String Slicing might help

Last edited 1 year ago by Arnav