首页 > 编程语言 > 详细

判断字符串是否为回文 python

时间:2018-05-28 13:21:26      阅读:215      评论:0      收藏:0      [点我收藏+]

回文正序和逆序一样的字符串,例如abccba

方法一

def is_palindrome1(text):
    l = list(text)
    l.reverse()
    t1 = ‘‘.join(l)
    if t1 == text:
        print ‘the text is palindrome‘
    else:
        print ‘the text is not palindrome‘

方法二

def is_palindrome2(text):
    t1 = text[::-1]
    if t1 == text:
        print ‘the text is palindrome‘
    else:
        print ‘the text is not palindrome‘

方法三

def is_palindrome3(text):
    r = True
    for x in range(len(text)):
        print x,text[x]
        if text[x] != text[len(text)-x-1]:
            print 1
            r = False
            break
    if r == True:
        print ‘the text is palindrome‘
    else:
        print ‘the text is not palindrome‘

判断字符串是否为回文 python

原文:https://www.cnblogs.com/xiaomingtx/p/9099600.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!