Learning Resources

User input

Input from user

# user_input.py
def reverse(text):
    return text[::-1]
def is_palindrome(text):
    return text == reverse(text)
something = input('Enter text: ')
if (is_palindrome(something)):
    print("Yes, it is a palindrome")
    print("No, it is not a palindrome")


   $ python user_input.py
   Enter text: sir
   No, it is not a palindrome
   $ python user_input.py
   Enter text: madam
   Yes, it is a palindrome
   $ python user_input.py
   Enter text: racecar
   Yes, it is a palindrome

How It Works:

We use the slicing feature to reverse the text. We've already seen how we can make slices from sequences using the seq[a:b] code starting from position a to position b. We can also provide a third argument that determines the step by which the slicing is done. The default step is 1 because of which it returns a continuous part of the text. Giving a negative step, i.e., -1 will return the text in reverse.

The input() function takes a string as argument and displays it to the user. Then it waits for the user to type something and press the return key. Once the user has entered and pressed the return key, the input() function will then return that text the user has entered.

We take that text and reverse it. If the original text and reversed text are equal, then the text is a palindrome.

Homework exercise:

Checking whether a text is a palindrome should also ignore punctuation, spaces and case. For example, "Rise to vote, sir." is also a palindrome but our current program doesn't say it is. Can you improve the above program to recognize this palindrome?

Hint (Don't read)

Use a tuple (you can find a list of all punctuation marks here punctuation) to hold all the forbidden characters, then use the membership test to determine whether a character should be removed or not, i.e. forbidden = ('!', '?', '.', ...).