check palindrome in python code example
Example 1: string palindrome in python
n = input("Enter the word and see if it is palindrome: ")
if n == n[::-1]:
print("This word is palindrome")
else:
print("This word is not palindrome")
Example 2: how to print palindrome in 100 between 250 in python
>>> def isPalindrome(s):
''' check if a number is a Palindrome '''
s = str(s)
return s == s[::-1]
>>> def generate_palindrome(minx,maxx):
''' return a list of Palindrome number in a given range '''
tmpList = []
for i in range(minx,maxx+1):
if isPalindrome(i):
tmpList.append(i)
return tmpList
>>> generate_palindrome(1,120)
[1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 22, 33, 44, 55, 66, 77, 88, 99, 101, 111]
Example 3: Write a function that tests whether a string is a palindrome
def palindrome_check(string):
string = list(string)
tmp = []
for x in range(0, len(string)):
if(string[x] != " "):
tmp.append(string[x].lower())
array1 = []
i = 0
j = len(tmp)-1
while(i < len(tmp)):
array1.append(tmp[j])
i += 1
j -= 1
counter = 0
for x in range(0, len(tmp)):
if(tmp[x] == array1[x]):
counter += 1
if(counter == len(tmp)):
return True
return False
Example 4: palindrome number + python
number=int(input("Enter any number :"))
temp=number
reverse_num=0
while(number>0):
digit=number%10
reverse_num=reverse_num*10+digit
number=number//10
if(temp==reverse_num):
print("The number is palindrome!")
else:
print("Not a palindrome!")
Example 5: palindrome python
def is_palindrome(w):
if w==w[::-1]:
print("Given String is palindrome")
else:
print("Given String is not palindrome")
is_palindrome("racecar")
Example 6: palindrome words python
mes=input("Enter the word and see if it is palindrome ")
if mes==mes[::-1]:
print("This word is palindrome")
else:
print("This word is not palindrome")