
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Check If a Given String is a Number Palindrome in Python
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