Previously we have developed palindrome numbers in python and palindrome strings in python. Now in this post, we will develop a palindrome program in python using for loop. We will check if the given number is a palindrome number or not. Also, We will check if the given string is a palindrome string or not.
Palindrome Number in Python using For Loop
Palindrome number:- If the Reverse of a number is equal to the same number then the number is called a palindrome number.
Example of palindrome number:-
19091 = 19091 So, 19091 is a palindrome number.
84964 != 46948 So, 84964 is not a palindrome number.
We will take integer numbers while declaring the variables. Then, check if the number is equal to the reverse number or not using the if-else statement. Finally, the result will be displayed on the screen.
# Palindrome number in python using for loop
# take inputs
num = input('Enter the number: ')
# check number is palindrome or not
i = 0
length = len(num)
if(len(num) % 2 == 0):
length = len(num) + 1
for i in range(length):
if num[i] != num[-1-i]:
print(num,'is not a Palindrome')
break
else:
print(num,'is a Palindrome')
Output for the input values test-case-1:-
Enter the number: 454
454 is a Palindrome
Output for the input values test-case-2:-
Enter the number: 12345
12345 is not a Palindrome
Output for the input values test-case-3:-
Enter the number: 1231
1231 is not a Palindrome
Palindrome Number Program in Python using For Loop & Function
We can also take the help of a function to check the palindrome number in python. A function is a block of code that performs a specific task.
# Palindrome number in python using for loop
def isPalindrome(num): #user-defined function
# Run loop from 0 to len/2
for i in range(0, int(len(num)/2)):
if num[i] != num[len(num)-i-1]:
return False
return True
# take inputs
num = input('Enter the number: ')
# calling function and display result
reverse = isPalindrome(num)
if reverse:
print(num,'is a Palindrome')
else:
print(num,'is not a Palindrome')
Output:-
Enter the number: 1596951
1596951 is a Palindrome
Output for the input values test-case-2:-
Enter the number: 1231
1231 is not a Palindrome
Palindrome Program in Python using For Loop
Palindrome string:- If the reverse of the string is the same string then the string is called a palindrome string.
Some examples of palindromic words are civic, radar, redivider, noon, level, rotor, racecar, redder, kayak, reviver, madam, and refer.
# Palindrome program in python using for loop
# take inputs
string = input('Enter the string: ')
# check string is palindrome or not
i=0
for i in range(len(string)):
if string[i]!=string[-1-i]:
print(string,'is not a Palindrome')
break
else:
print(string,'is a Palindrome')
Output for the input values test-case-1:-
Enter the string: civic
civic is a Palindrome
Output for the input values test-case-2:-
Enter the string: Know Program
Know Program is not a Palindrome
Palindrome String Program in Python using For Loop
We can also take the help of a function to check the palindrome program in python. A function is a block of code that performs a specific task.
# Palindrome program in python using for loop
def isPalindrome(s): #user-defined function
# Run loop from 0 to len/2
for i in range(0, int(len(s)/2)):
if s[i] != s[len(s)-i-1]:
return False
return True
# take inputs
string = input('Enter the string: ')
# calling function and display result
reverse = isPalindrome(string)
if reverse:
print(string,'is a Palindrome')
else:
print(string,'is not a Palindrome')
Output:-
Enter the string: radar
radar is a Palindrome
If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!
Your logic for the palindrome number is wrong. In a test case, it will not pass if you input 1231 then your output=palindrom. Buts it’s wrong.
We have updated the program. Kindly check it now.