In this tutorial, you will learn the writing Python program to check if a string is a palindrome or not.
Read This: What is String Palindrome? Write string palindrome program in C.
How this Python program will behave?
To check String is Palindrome or not? This Program will take a String as an input. And after applying some logic it will return output as String is Palindrome or not.
For Example:
Suppose if we give input a string “madam”. This is palindrome String then our program will print “Given string is palindrome”.
And if we give “abcd” then our program will give “Given String is not palindrome”.
Palindrome program of String in Python
Using reverse and compare
Below program is very simple. Here first we will reverse the original input string and then compare it with original input.
If both string, before reverse and after reverse is equal then print String is Palindrome otherwise not.
To reverse string in this program [:: -1] is used.
To compare string, == is used in if-else statement.
string = input("Please give a String : ")
if(string == string[:: - 1]):
print("Given String is a Palindrome")
else:
print("Given String is not a Palindrome")
Output:
Please give a String : madam
Given String is a Palindrome
Using method and for loop
In this below program we have built some different logic to check string is palindrome or not.
Here using for loop we match each character. Here we will match first character with last character. Second character with second last character and so on..
Loop will be execute only half time of the length of string.
Here we have declared one method isPalindrome(string).
def isPalindrome(string):
for i in range(0, int(len(string)/2)):
if string[i] != string[len(string)-i-1]:
return False
return True
string = input("Please give a String : ")
if (isPalindrome(string)):
print("Given String is a Palindrome")
else:
print("Given String is not a Palindrome")
Output
Please give a String : madam
Given String is a Palindrome
Similar Reads
-
Find middle element of a linked list in single pass
Linked lists are a fundamental data structure used in computer science for organizing and managing data. A linked list is… -
Python Program to Find Last 3rd element in Singly Linked List
In this tutorial, we are going to learn the writing python program to Find 3rd element of Linked List from… -
Most important JavaScript Interview Questions To Prepare
In this Page we have collected and explained Most important Javascript Interview Questions and Answers for begineers, freshers as well… -
Sum of digits of Given Number in Java
In this tutorial we will learn writing Java program to calculate the sum of its digit. We will also see… -
Hibernate Interview Questions for 2+ years of experience
Certainly! Here's a list of commonly asked interview questions on Hibernate for candidates with 2+ years of experience: Basic Hibernate… -
68 Most Important Microservices Interview Questions
Certainly, here's an extended list of 50 commonly asked interview questions on microservices for candidates with 2+ years of experience:… -
60 Most Important Git Interview Questions
Certainly! Here is a list of commonly asked interview questions on Git for candidates with fresher or having of experience… -
50+ Most important Java Interview Questions for 5+ Years Exp
1. Explain the SOLID principles in Java. Provide examples of how you have applied these principles in your projects. SOLID… -
60+ Spring Boot interview questions for 4+ years Exp.
1. What is Spring Boot and how does it differ from the Spring framework? Spring Boot is a framework designed… -
60+ Mostly Asked Spring Boot Interview Questions for 3+ Yrs
Here is a list of 60+ Spring Boot interview questions for candidates with 3+ years of experience: 1. What is…