您的位置:首页 > 编程语言 > Python开发

python程序:检查字符串是否是回文(2)

2017-05-22 12:54 381 查看
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!')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python 回文