Write a Python Program to Check Whether a String is Palindrome or Not

Palindrome is a word or a sequence of characters that reads the same forward and backward.

For example, “racecar” and “level” are palindromes.

In this tutorial, we will see how to write a Python program to check whether a given string is a palindrome or not.


First, let’s understand the steps to check if a string is a palindrome or not.

We can reverse the given string and check if it is equal to the original string.

If they are equal, then the string is a palindrome, otherwise not.

Here’s the Python program to check whether a string is palindrome or not:

def is_palindrome(s):
    return s == s[::-1]

# Testing the function
string = input("Enter a string: ")

if is_palindrome(string):
    print(f"{string} is a palindrome")
else:
    print(f"{string} is not a palindrome")

In the above code, we have defined a function called is_palindrome that takes a string as input and returns True if the string is a palindrome, otherwise False.

The [::-1] syntax is used to reverse the string.

We then prompt the user to enter a string and call the is_palindrome function to check if it is a palindrome.

Based on the return value of the function, we print whether the string is a palindrome or not.

Let’s test the program with some sample inputs:

Enter a string: racecar
racecar is a palindrome

Enter a string: level
level is a palindrome

Enter a string: python
python is not a palindrome

In conclusion, we can check whether a given string is a palindrome or not by comparing it with its reverse.

We can do this easily in Python using string slicing.