Python Program to Check String is Palindrome using Stack

This is a Python program to check whether a string is a palindrome using a stack.

Problem Description

The program prompts the user for a string and determines whether it is a palindrome with the help of a stack.

Problem Solution

1. Create a class Stack with instance variable items initialized to an empty list.
2. Define methods push, pop and is_empty inside the class Stack.
3. The method push appends data to items.
4. The method pop pops the first element in items.
5. The method is_empty returns True only if items is empty.
6. Prompt the user for a string and push all the characters to a stack.
7. Construct the reversed string by popping characters from the stack.
8. Deterime whether the string is palindromic by comparing the two strings.

Program/Source Code

Here is the source code of a Python program to check whether a string is a palindrome using a stack. The program output is shown below.

class Stack:
    def __init__(self):
        self.items = []
 
    def is_empty(self):
        return self.items == []
 
    def push(self, data):
        self.items.append(data)
 
    def pop(self):
        return self.items.pop()
 
 
s = Stack()
text = input('Please enter the string: ')
 
for character in text:
    s.push(character)
 
reversed_text = ''
while not s.is_empty():
    reversed_text = reversed_text + s.pop()
 
if text == reversed_text:
    print('The string is a palindrome.')
else:
    print('The string is not a palindrome.')
Program Explanation

1. An instance of Stack is created.
2. The user is prompted to enter a string.
3. The characters of the string are pushed to the stack.
4. The reversed string is constructed by popping the characters from the stack.
5. If the reversed string and the original string are the same, then the string is palindromic.
6. The result is displayed.

advertisement
Runtime Test Cases
Case 1:
Please enter the string: madam
The string is a palindrome.
 
Case 2:
Please enter the string: racecar
The string is a palindrome.
 
Case 3:
Please enter the string: palace
The string is not a palindrome.

Sanfoundry Global Education & Learning Series – 1000 Python Programs.

If you wish to look at all Python Programming examples, go to 1000 Python Programs.

👉 Join Sanfoundry classes at Telegram or Youtube

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.