Palindrome Program in Python using For Loop

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
for i in range(len(num)):
   if num[i]!=num[-1-i]:
      print(num,'is not a Palindrome')
      break
   else:
      print(num,'is a Palindrome')
      break

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

Palindrome Number Program in Python using For Loop

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

Palindrome Program in Python using For Loop

Palindrome string:- If the reverse of the string is the same string then the string is called 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')
      break

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

Leave a Comment

Your email address will not be published.