- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Check String is Palindrome using Stack
When it is required to check if a string is a palindrome using stack data structure, a stack class is created, and push and pop methods are defined to add and delete values from stack. Another method checks to see if the stack is empty or not.
Below is a demonstration for the same −
class Stack_structure: def __init__(self): self.items =  def check_empty(self): return self.items ==  def push_val(self, data): self.items.append(data) def pop_val(self): return self.items.pop() my_instance = Stack_structure() text_input = input('Enter the string... ') for character in text_input: my_instance.push_val(character) reversed_text = '' while not my_instance.check_empty(): reversed_text = reversed_text + my_instance.pop_val() if text_input == reversed_text: print("The string is a palindrome") else: print("The string isn't a palindrome")
Enter the string... MalayalaM The string is a palindrome
A class named ‘Stack_structure’ is defined with ‘init’ method.
This method initializes an empty list.
Another method named ‘check_empty’ is defined that checks if the stack is empty or not.
Another method named ‘push_val’ is defined that adds elements to the stack.
Another method named ‘pop_val’ is defined that deletes elements from the stack.
An instance of this ‘Stack_structure’ is defined.
The string is taken from the user.
It is iterated over, and the ‘check_empty’ method is called on it.
Another empty string is defined, and the string is reversed.
This reversed string is stored in the empty string.
This reversed string and the string from the user are compared.
If they are same, it means it is a palindrome.
Otherwise, it is not a palindrome.
The relevant output is displayed on the console.
- Python program to check if a string is palindrome or not
- Python program to check if a given string is number Palindrome
- Python program to check if the given string is vowel Palindrome
- Program to check a string is palindrome or not in Python
- Python program to check whether the string is Symmetrical or Palindrome
- Python Program to Check Whether a String is a Palindrome or not Using Recursion
- How to check if String is Palindrome using C#?
- How to Check Whether a String is Palindrome or Not using Python?
- Java program to check string as palindrome
- Program to check string is palindrome with lowercase characters or not in Python
- Program to check string is palindrome or not with equivalent pairs in Python
- Python program to check if binary representation is palindrome?
- C# program to check if a string is palindrome or not
- C Program to Check if a Given String is a Palindrome?
- Check if a string is palindrome in C using pointers