How to Check If a String is a Palindrome in Python


In this example, we will learn how to check if a string is a palindrome.

Source Code

#! /usr/bin/env python3
# -*- coding: utf-8 -*-

str = input("Please input the string: ")
if str == str[::-1]:
    print("This string is a palindrome.")
else:
    print("This string is not a palindrome.")

Output:

Case 1:
Please input the string: hello world
This string is not a palindrome.

Case 2:
Please input the string: racecar
This string is a palindrome.
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
Advertisement