String Palindrome program in python
In this tutorial, you will learn the writing Python program to check if a string is a palindrome or not.
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.
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")
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")
Also prepare these Python Programs:
Latest Uploads on Website
- AVL Tree with explanation
- Radix sort algorithm explanation with example
- Quick Sort Algorithm with explanation
- Bubble sorting algorithm with Bubble sort program in C
- Insertion sort algorithm and program in C
- Selection Sort Algorithm and Program in C
- Linear probing technique explanation with example
- Collision in Hashing and Collision resolution technique
- Hashing in data structure with its types
- Binary search tree operations with Program
- Binary search tree in data structure
- Binary search algorithm in data structure with explanation
- linear search in data structure with Algo and Program