Practice Question 6
In this question, we will read a text file and find all the palindromic words present in it. A palindrome is a word that reads the same backward as forward, such as “level”, “noon”, or “radar”.
The Logic (How it works)
Before writing the code, let us first understand the logic behind the solution.
To identify palindromes in a file, we need to treat the file as a collection of words. Here is the step-by-step logic:
-
Open & Split: We open
letter.txtand use the.split()method to turn the entire file content into a list of individual words. -
Normalization: We convert each word to lowercase using
.lower(). This ensures that ‘Madam’ and ‘madam’ are treated as the same word (ignoring case sensitivity). -
The Reverse Check: We use the Python slicing trick
[::-1]to reverse the word. -
Match & Display: If the original word is exactly the same as the reversed word, it is a palindrome, and we print it.
Let’s Code as per the above logic :
def displayPalindromicWords():
f = open("letter.txt", "r")
data = f.read()
wordlist = data.split()
for word in wordlist:
word = word.lower()
# Reverse the word using slicing [::-1]
if word == word[::-1]:
print(word)
f.close()
# To call the function:
displayPalindromicWords()
Important Notes for Students
As a teacher, I want you to pay close attention to these points, as they are where most students make mistakes in exams:
-
The
[::-1]Slice: This is the most “Pythonic” way to reverse a string. The first two colons mean “start to end,” and the-1means “step backward.” -
Why
wordlist? By usingdata.split(), we automatically ignore spaces and newlines, focusing only on the actual words. -
Lowercasing early: Doing
word = word.lower()at the top of the loop is a smart move. It makes theifcondition much cleaner and easier to read.
Teacher’s Secret Tip for Exams:
If the examiner asks you to check palindromes without ignoring case, simply remove .lower(). Then Madam and madam will be treated as different words.
If the examiner asks you to count how many palindromic words are present, just add a counter variable and increase it whenever a palindrome is found.
If the examiner asks you to store all palindromic words, you can use a list instead of printing them and append each palindrome to it.
If the examiner asks you to check palindromic numbers instead of words, the same logic works — you only need to read digits instead of words.
If the examiner asks you to find Palindromic Lines instead of words, simply change data.split() to f.readlines(). The rest of the logic (lowercasing and reversing) remains exactly the same.
Want to practice more? If you found this helpful, I have compiled a full list of 30 practice problems just like this one. Each question focuses on a different logic to help you become a pro at File Handling.
Click here to view the full 30-Question Practice Set
Exam Special Recommendation: If you are studying in 12th class and preparing for your board exam, then go for the “Computer Science with Python Sample Paper Book“. It contains 3 previous years’ papers and 7 practice papers solved strictly as per the CBSE pattern. Click here to purchase your copy!