WAP (Python) to check if a string/number is a palindrome or not - BoiCoder

BoiCoder

Coding Made Easy for All

Breaking

Sunday 27 September 2020

WAP (Python) to check if a string/number is a palindrome or not


Hey all, today we are going to check if a string/number is a palindrome or not. So gear up, as we are going to do this program in the best way possible!


As always, you can find the final code at the end. But it would be good if you stay with us as we are going go through each and every step you need to follow to become the best coder! This is literally going to be the easiest solution you would have ever seen!!!


* We are going to make a single program for both numbers and strings!*


What is a Palindrome?


If any string/number is same as its reverse, then its called a palindrome. Let's take a simple example:


a) Numbers: 343, 545, are some of the numbers that are equal to their reverse.


b) Strings: 'Madam', 'Nitin' are some strings that are same as their reverse.


I hope you are now able to understand what a palindrome is. Now let us start with the overview!


Overview:


In this program, we are asked to find if a string/number is a palindrome or not. So first, we need to ask the user for input. Next we would use simple slicing technique to solve this beautiful problem!


*Now that I have told you the basics of the problem, you could try it on your own!*


Understanding what we need to do:


Here I will tell you step by step what you need to do.


Step 1: Asking the user for the input:


This is the easiest part. We just have to ask the user for an input. I have taken a variable 'value' for this.


Also read: WAP (Python) to find whether the number entered by the user is a prime number or not


Here's the code for this step:


Step 2: Checking if the string/number is a palindrome or not:


Now I am going to tell you the simple concept of slicing. In python, if you want to get individual elements of a string, you can easily get them using slicing. The basic syntax for slicing would be something like this: [start: end: step]


Learn more about it here: Array Slicing - W3schools


Now you would have got the basic idea behind this program. But the above syntax gives us only one element at a time, but in our program, we need it to repeatedly give all the elements of the string. To do this, either we could use loops or better, we have an inbuilt method for this particular task. 


It's a part of slicing itself, its syntax is:

[start point:: n], which means staring from [start point] it will print every nth term.


Refer to this for forum for more info: Double colon in python - Stack Overflow


Now, in the program, we would just check a condition if the 'value' is equal to 'value[::-1]' or not. What this expression does is that, it returns every element starting from the back (as '-' sign denotes 'from back'). Hence with this simple line of code we could check if a string/number is a palindrome or not. If this condition satisfies, then it will be a palindrome, if it doesn't then it won't be a palindrome!


* Note that we don't need two program for strings and numbers. Numbers can also be treated like strings. Hence, even if you put a number as the input, the program would give the correct output!*


Here's the code for this step:


Now, we have successfully found the best solution to this problem!


Here's the final code:


And here ends our amazing journey where we found the best way to check if a number/string is a palindrome or not. I really hope you liked this article and if you did, then don't forget to share it as it can help someone struggling with this question!


Also read: WAP (python) to find the factorial of a given number


Also please consider subscribing to our newsletter as we do these coding sessions regularly!


Again, thanks for reading!!!

No comments:

Post a Comment