A palindrome is a word, number, or sequence that reads the same forward and backward. For example, 121 is a palindrome because it remains 121 when its digits are reversed. Similarly, the word madam is a palindrome because it reads the same from both directions. Writing a palindrome program in Python helps beginners understand strings, loops, conditional statements, and number manipulation.
Table of Contents
What is a Palindrome?
A palindrome is a word, number, or sequence that remains the same when it is reversed.
For example, 121 is a palindrome because its reverse is also 121.
Similarly, madam is a palindrome because:
Forward: madam
Reverse: madam
If the original value and the reversed value are the same, the value is considered a palindrome.
How Does a Palindrome Program Work?
A palindrome program follows a simple process to compare a value with its reverse.
- Take the Input: The program accepts a word or number from the user.
- Create the Reverse: The program reverses the entered value.
- Compare the Values: The original value is compared with the reversed value.
- Display the Result: If both values are the same, the program displays that the value is a palindrome.
Examples of Palindromes
Some common palindrome numbers are 121, 131, 151, 202, 343, and 1221.
Some common palindrome words are madam, level, radar, and civic.
For example:
1221 → Reverse = 1221
Since the original number and its reverse are the same, 1221 is a palindrome.
Checking a Palindrome Using String Slicing
Python provides string slicing that can be used to reverse a string. The [::-1] slicing technique returns the string in reverse order.
# Python program to check whether a number is a palindrome using string slicing
number = input("Enter a number: ")
reverse = number[::-1]
if number == reverse:
print("The number is a palindrome.")
else:
print("The number is not a palindrome.")
Output:
Enter a number: 121
The number is a palindrome.
In this program, the number is taken as a string so that string slicing can be used to create its reverse.
Checking a Number Palindrome Using a While Loop
A palindrome number can also be checked without converting it into a string. The % and // operators can be used to extract the digits and construct the reversed number.
# Python program to check whether a number is a palindrome using a while loop
number = int(input("Enter a number: "))
original = number
reverse = 0
while number > 0:
digit = number % 10
reverse = reverse * 10 + digit
number = number // 10
if original == reverse:
print("The number is a palindrome.")
else:
print("The number is not a palindrome.")
Output:
Enter a number: 1221
The number is a palindrome.
The % 10 operation extracts the last digit, while // 10 removes the last digit. The program gradually constructs the reversed number.
Checking a Palindrome Using a For Loop
A for loop can be used to create the reverse of a string by processing its characters.
# Python program to check whether a string is a palindrome using a for loop
text = input("Enter a word: ")
reverse = ""
for character in text:
reverse = character + reverse
if text == reverse:
print("The word is a palindrome.")
else:
print("The word is not a palindrome.")
Output:
Enter a word: level
The word is a palindrome.
In each iteration, the current character is added before the existing characters in reverse. This creates the reversed string.
Checking a String Palindrome
A palindrome program can also be used to check words or sentences. The input can be converted to lowercase so that uppercase and lowercase letters are treated equally.
# Python program to check whether a word is a palindrome
text = input("Enter a word: ")
text = text.lower()
reverse = text[::-1]
if text == reverse:
print("The word is a palindrome.")
else:
print("The word is not a palindrome.")
Output:
Enter a word: Madam
The word is a palindrome.
The lower() method converts the input to lowercase before the comparison.
Complete Palindrome Program
The following program checks whether the entered number is a palindrome and also handles negative numbers.
# Python program to check whether a number is a palindrome
number = int(input("Enter a number: "))
if number < 0:
print("Please enter a positive number.")
else:
original = number
reverse = 0
while number > 0:
digit = number % 10
reverse = reverse * 10 + digit
number = number // 10
if original == reverse:
print(original, "is a palindrome.")
else:
print(original, "is not a palindrome.")
Output:
Enter a number: 1331
1331 is a palindrome.
The program stores the original number in a separate variable before modifying the input number. It then reverses the number and compares the reversed value with the original value.
Advantages of Palindrome Programs
- Logical Thinking: Palindrome programs help beginners understand how to convert a problem statement into programming logic.
- Loop Practice: These programs provide practical practice with for and while loops.
- String Manipulation: String-based palindrome programs help beginners understand slicing, comparison, and string methods.
- Number Manipulation: Number-based palindrome programs demonstrate how individual digits can be extracted and processed.
- Problem-Solving Skills: Palindrome problems help beginners practice comparison and conditional logic.
Common Beginner Mistakes
- Not Storing the Original Number: The original number should be stored before changing it during the reversal process.
- Using Incorrect Digit Extraction: The % 10 and // 10 operators should be used correctly when reversing a number.
- Comparing Different Data Types: The original value and reversed value should be compared using compatible data types.
- Ignoring Uppercase Letters: When checking words, uppercase and lowercase letters can produce different results if the input is not normalized.
- Handling Negative Numbers Incorrectly: The program should clearly define how negative numbers are handled instead of treating them as normal positive palindrome numbers.
Conclusion
A palindrome program checks whether a word or number remains the same when it is reversed. Python provides several ways to solve this problem, including string slicing, for loops, and while loops. Practicing palindrome programs helps beginners improve their understanding of loops, strings, arithmetic operators, conditions, and basic problem-solving techniques.
Frequently Asked Questions
1. What is a palindrome?2. Is 121 a palindrome number?A palindrome is a word, number, or sequence that remains the same when it is read forward and backward.
3. Can a palindrome be checked without using string conversion?Yes. The reverse of 121 is also 121, so 121 is a palindrome number.
4. Which Python method can reverse a string?Yes. A number palindrome can be checked using a while loop, %, and // operators without converting the number into a string.
5. Can the palindrome program check words?String slicing using [::-1] can be used to create the reverse of a string.
Yes. The same concept can be used to check whether a word such as madam or level is a palindrome.
0 Comments