Check if given String is Palindrome in Python
Write a program that reads a string from the user and uses a loop to determines whether or not it is a palindrome. Display the result, including a meaningful output message. [A string is a palindrome if it is identical forward and backward.]
import re Continue = 1 Quit = 2 def main(): choice = 0 while choice != Quit: # Display the menu. display_menu() # Constant to assume string is Palindrome is_palindrome = True # Get the user's choice. choice = int(input('\nEnter your choice: ')) # Perform the selected action. if choice == Continue: line = input("\nEnter a string: ") str_lower = re.sub("[^a-z0-9]", "", line.lower()) for i in range(0, len(str_lower)//2): if str_lower[i] != str_lower[len(str_lower) - i - 1]: is_palindrome = False if is_palindrome: print(line, "is a palindrome") else: print(line, "is not a palindrome") else: print('Thank You.') def display_menu(): print('\n*******MENU*******') print('1) Continue') print('2) Quit') main()
Sample output of above program.
*******MENU*******
1) Continue
2) Quit
Enter your choice: 1
Enter a string: A dog! A panic in a pagoda!
A dog! A panic in a pagoda! is a palindrome
*******MENU*******
1) Continue
2) Quit
Enter your choice: 1
Enter a string: Civic
Civic is a palindrome
*******MENU*******
1) Continue
2) Quit
Enter your choice: 1
Enter a string: Python vs Java
Python vs Java is not a palindrome
*******MENU*******
1) Continue
2) Quit
Enter your choice: 2
Thank You.
2017-09-15T08:53:47+05:30
2017-09-15T08:53:47+05:30
Amit Arora
Amit Arora
Python Programming Tutorial
Python
Practical Solution