Data Science and Machine Learning Internship ...
- 22k Enrolled Learners
- Weekend/Weekday
- Live Class
As kids, it was fun reading reverse strings and when we grew up a little we learned that strings reading same either ways are called palindromes. Curiosity dint leaves us there, so we wanted our machines to learn what are palindromes and for all Python lovers, no other language can do it in a better way. If you are a python lover and a coding enthusiast, read along to learn how to check for a Palindrome number in Python using while loop and in-built functions.
This Edureka video on the ‘Python For Data Science Full Course’ will help you learn Python for Data Science including all the relevant libraries. Following are the topics discussed in this Python for Data Science tutorial
when checking for if a number is Palindrome or not with Python, we will be using while loop and in-built functions. For beginners, Edureka has a 360° Python course page which will help you learn Python from scratch even if you don’t have much coding knowledge.
Course Name | Upcoming Batches | Fees |
Pyspark Certification Training | 27th April,2024 (Weekend Batch) | ₹21,995 |
Pyspark Certification Training | 1st June,2024 (Weekend Batch) | ₹21,995 |
A palindrome is nothing but any number or a string which remains unaltered when reversed.
Example: 12321
Output: Yes, a Palindrome number
Example: RACECAR
Output: Yes, a Palindrome string
It is evident that letters form mirror images on reversal.
Now that you understood the concept, let’s simply dive into a program to check palindrome in Python.
Find out our Python Training in Top Cities/Countries
This is one of the easiest programs to find Palindrome program using while loop in Python Programming. Let’ dive into an example to check whether a given input is a palindrome or not.
num=int(input("Enter a number:")) temp=num rev=0 while(num>0): dig=num%10 rev=rev*10+dig num=num//10 if(temp==rev): print("The number is palindrome!") else: print("Not a palindrome!")
Output:
Enter a number:121
The number is palindrome!
Moving ahead in Python palindrome program examples, let’s us see how to check a string whether its a palindrome or not using built-in functions.
string=input(("Enter a string:")) if(string==string[::-1]): print("The string is a palindrome") else: print("Not a palindrome")
Output:
Explanation: In the above program, first take input from the user (using input OR raw_input() method) to check for palindrome. Then using slice operation [start:end:step], check whether the string is reversed or not. Here, step value of -1 reverses a string. If yes, it prints a palindrome else, not a palindrome.
Course Name | Upcoming Batches | Fees |
Python Certification Course | 20th April 2024 (Weekend Batch) | ₹17,795 |
Python Certification Course | 18th May 2024 (Weekend Batch) | ₹15,125 |
Make sure you practice as much as possible and revert your experience.
Got a question for us? Please mention it in the comments section of this “Palindrome program in Python” blog and we will get back to you as soon as possible, or you can join our Data science with Python course.
Enroll now in our comprehensive Python Course and embark on a journey to become a proficient Python programmer. Whether you’re a beginner or looking to expand your coding skills, this course will equip you with the knowledge to tackle real-world projects confidently.
Discover your full abilities in becoming an AI and ML professional through our Artificial intelligence course. Learn about various AI-related technologies like Machine Learning, Deep Learning, Computer Vision, Natural Language Processing, Speech Recognition, and Reinforcement learning.
Learn about Jupyter Notebook Cheat Sheet
Course Name | Date | Details |
---|---|---|
Data Science with Python Certification Course | Class Starts on 4th May,2024 4th May SAT&SUN (Weekend Batch) | View Details |
Data Science with Python Certification Course | Class Starts on 25th May,2024 25th May SAT&SUN (Weekend Batch) | View Details |
edureka.co