1. 程式人生 > >python程式:檢查字串是否是迴文(2)

python程式:檢查字串是否是迴文(2)

#!/usr/bin/python
#Filename: user_input_1.py
#Function: to check whether the string is palindrome or not. Ignore space(空格), case(大小寫) and punctuation(標點符號).
#Test string: "Rise to vote,sir."


import string


def reverse(text):
  return text[::-1]


def is_palindrome(text):
  text = text.lower()
  text = text.replace(' ', '')
  for char in string.punctuation:
    text = text.replace(char, '')
  return text == reverse(text)


def main():
    something = input('Enter text:')
    if (is_palindrome(something)):
      print('Yes, "{0}" is a palindrome.'.format(something))
    else:
      print('No, "{0}" is not a palindrome.'.format(something))


if __name__ == '__main__':
  main()
else:
  print('user_input_1.py was imported!')