► Python program to find if a string is palindrome or not(using for loop)


palindrome is a word, number, phrase, or other sequence of characters which reads the same backward as forward

Steps followed:
1. take input from user and store in var variable
2. reverse the input string using for loop and store in rev variable
3. print reverse string
4. compare input string and reversed string to check for palindrom


Program

#python program to find if a string is palindrome or not(using for loop)

#getting input from user and storing in var variable
var=input("Enter a string: ")

length=len(var)
rev=""

#reverse the input string and store in rev variable
for i in var:
    rev+=var[length-1]
    length -= 1
    

#print reverse string
print("reverse of input string %s"%(rev))

#compare input string and reverse string to check for palindrom
if var == rev:
    print("Palindrome")
else:
    print("Not Palindrome")


Output

Enter a string: computer
reverse of input string retupmoc
Not Palindrome

 

Enter a string: malayalam
reverse of input string malayalam
Palindrome





Also Read: