Python Program to Check if a String is a Palindrome

In this tutorial, we will discuss a Python program to check if a string is a Palindrome.

Before going to the program first, let us understand what is Palindrome.

Palindrome:

  • A palindrome is a sequence of characters that reads the same backward as forward.

Related: Python program to count vowels & consonants in a string

Program code to check if a string is a palindrome in Python

# Check for Palindrome in a String in Python
def is_palindrome(string):
    return string == string[::-1]

input_string = input("Enter a string: ")
if is_palindrome(input_string):
    print(f"'{input_string}' is a palindrome.")
else:
    print(f"'{input_string}' is not a palindrome.")

Explanation

  1. Function Definition: The is_palindrome function takes a string as input and returns True if the string is a palindrome, otherwise False.
  2. Main Program: The program prompts the user to enter a string and then checks if the string is a palindrome using the is_palindrome function and prints the result.

Output

Python Program to Check if a String is a Palindrome

  • When you run the above program, it will prompt you to enter a string.
  • After entering the string, it will check if the string is a palindrome and print the result.

Conclusion

  • In this tutorial, we learned how to check if a string is a palindrome using a Python program.
  • Understanding this concept is essential for string manipulation and enhancing your programming skills.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *