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!')
#!/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程序:检查字符串是否是回文(1)
- python检查字符串是否是正确ISBN的方法
- python检查字符串是否是正确ISBN的方法
- 设计一个程序,判定输入的字符串是否回文
- 判断字符串是否为回文 python实现
- 编写程序,判断输入的字符串是否回文
- 49.输入一字符串,检查是否回文 (回文是指正反序相同,如,LeveL)
- Python3基础 str endswith 检查字符串是否以指定字符串结束
- 用一个Python程序来判断一个字符串是否是另一个字符串的sublist
- python cookbook 学习笔记 -- 1.8 检查字符串中是否包含某字符集合中的字符
- FCC--Check for Palindromes(检查字符串是否为回文)
- 检查输入的字符串是否为回文 当一个字符串顺读倒读时都一样,则这样的字符串就是回文了 例如 123321就是
- 检查字符串是否为合法的Java标识符的程序
- Quzh[python]判断是否为回文——判断用户输入的字符串是否为回文。
- python删除所有的中文字符、非ASCII或非英文字符,检查字符串是否包含非ASCII
- 【python cookbook】 检查字符串中是否包含某自符集合中的字符
- 001_008 Python 检查字符串中是否包含某字符集合中的字符
- python程序2(递归查找某一个文件夹下所有的文件是否含有某个特定的字符串,并打印该文件名)
- Java——递归检查字符串是否回文