
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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
- Related Articles
- Python program to check if the given string is vowel Palindrome
- C Program to Check if a Given String is a Palindrome?
- Python program to check if a string is palindrome or not
- TCP Client-Server Program to Check if a Given String is a Palindrome
- Bash program to check if the Number is a Palindrome?
- Check if it is possible to create a palindrome string from given N in Python
- C# program to check if a string is palindrome or not
- Python Program to Check String is Palindrome using Stack
- Program to check a string is palindrome or not in Python
- Python program to check if the given number is a Disarium Number
- Python program to check if binary representation is palindrome?
- Palindrome in Python: How to check a number is palindrome?
- Check if a given string is a valid number in Python
- Program to check a number is palindrome or not without help of a string in Python
- Python program to check if the given string is pangram
