Given a string, please determine whether it is a palindrome. For example, 12321 is a palindrome. At this time, the same sequence of characters is obtained for both sequential reading and backward reading, but abcca is not.


# Method one, recursive slicing method
def is_palindrom (s):
"" "To judge the number of palindromes, recursive method" ""
if len (s) <2:
return True
if s [0] == s [-1]:
return is_palindrom (s [1: -1])
else:
return False


name1 = 'ABBA'
name2 = '1234'
print (is_palindrom (name1))
print (is_palindrom (name2))


# Method two, according to the literal understanding, put the head and tail Compare in turn:
s = input ('Please enter a string:')
if not s:
print ('Please do not enter an empty string!')
S = input ('Please re-enter a string:')
a = len ( s)
i = 0
count = 1
while i <= (a / 2):
if s [i] == s [ai-1]:
count = 1
i + = 1
else:
count = 0
break
if count == 1 :
print ('The string you entered is a palindrome')
else:
print ('The string you entered is not a palindrome')


# Method three, use the reversed () function method
s = input ('Please enter a string:')
if not s:
print ('Please do not enter an empty String! ')
S = input (' Please re-enter a string: ')
a = reversed (list (s))
if list (a) == list (s):
print (' The string you entered is Palindrome ')
else:
print (' The string you entered is not a palindrome ')

Guess you like

Origin www.cnblogs.com/zhaoyiyao/p/12707640.html