判斷字符串是否為回文 python


回文正序和逆序一樣的字符串,例如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'


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM