Python program to check if a given string is number Palindrome


In this article, we will learn about the solution and approach to solve the given problem statement.

Problem statement

Given a string input, we need to create a python function to check whether it is a palindrome or not.

A string is referred to be palindrome if the reverse of the string is identical with string.

We can do this by two methods −

  • Reversal by slicing
  • Comparison via negative indexing

Here we will be learning reversal pf string bu the help of slicing.

To reverse a string by th method of slicing specify the following statement −

Str[ : : -1 ]

Where the start and end parameters are unassigned and step value is -1.

Now let’s see the implementation −

Example

num = input('Enter any number : ')
try:
   val = int(num)
   if num == str(num)[::-1]:
      print('The given number is PALINDROME')
   else:
      print('The given number is NOT a palindrome')
except ValueError:
   print("That's not a valid number, Try Again !")

Output

Enter any number : 78287
The given number is PALINDROME

Here we used exception handling to ensure that input string contains only numeric characters.

Conclusion

In this article, we learnt about the approach to find whether a string is a number palindrome or not

Updated on: 26-Sep-2019

6K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements