📖 Scenario: You are building a simple program to check if a word is a palindrome. A palindrome is a word that reads the same forwards and backwards, like 'radar' or 'level'. This is useful in games, puzzles, and text analysis.
🎯 Goal: Build a program that takes a word, checks if it is a palindrome, and prints the result.
📋 What You'll Learn
Create a variable with a specific word
Create a helper variable for the reversed word
Compare the original and reversed word
Print whether the word is a palindrome or not
💡 Why This Matters
🌍 Real World
Palindrome detection is used in text processing, coding puzzles, and checking symmetrical patterns in data.
💼 Career
Understanding string manipulation and condition checks is fundamental for programming jobs, especially in software development and data analysis.
Progress0 / 4 steps
1
Create the word variable
Create a variable called word and set it to the string 'racecar'.
DSA Python
Hint
Use single quotes around the word 'racecar'.
2
Create the reversed word variable
Create a variable called reversed_word that stores the reverse of the string in word using slicing word[::-1].
DSA Python
Hint
Use word[::-1] to reverse the string.
3
Check if the word is a palindrome
Create a variable called is_palindrome that is True if word is equal to reversed_word, otherwise False.
DSA Python
Hint
Use the equality operator == to compare the two strings.
4
Print the palindrome check result
Print "Palindrome" if is_palindrome is True, otherwise print "Not a palindrome".
DSA Python
Hint
Use an if statement to check is_palindrome and print the correct message.
Practice
(1/5)
1. What does it mean if a string is a palindrome?
easy
A. It contains only letters
B. It reads the same forwards and backwards
C. It has an even number of characters
D. It starts and ends with the same letter
Solution
Step 1: Understand palindrome definition
A palindrome is a word or phrase that reads the same forwards and backwards.
Step 2: Compare options with definition
Only It reads the same forwards and backwards matches this definition exactly.
Final Answer:
It reads the same forwards and backwards -> Option B
Quick Check:
Palindrome = Same forwards and backwards [OK]
Hint: Check if string equals its reverse [OK]
Common Mistakes:
Thinking palindrome means only letters
Assuming even length only
Confusing start/end letters with palindrome
2. Which of the following Python code snippets correctly checks if a string s is a palindrome?
easy
A. s == reversed(s)
B. s == s[1:]
C. s == s.reverse()
D. s == s[::-1]
Solution
Step 1: Understand string reversal in Python
The slice s[::-1] returns the reversed string.
Step 2: Check each option
s == s[::-1] compares s to its reversed slice correctly. s == reversed(s) compares to an iterator, not a string. s == s.reverse() tries to use reverse() which is a list method and returns None. s == s[1:] compares to a substring missing the first character.
Final Answer:
s == s[::-1] -> Option D
Quick Check:
Use slicing to reverse string [OK]
Hint: Use s == s[::-1] to check palindrome [OK]
Common Mistakes:
Using reversed() without join
Calling reverse() on string
Comparing to wrong substring
3. What will be the output of the following code?
def is_palindrome(s):
return s == s[::-1]
print(is_palindrome('level'))
print(is_palindrome('world'))
medium
A. True\nFalse
B. False\nTrue
C. True\nTrue
D. False\nFalse
Solution
Step 1: Check 'level' palindrome status
'level' reversed is 'level', so is_palindrome('level') returns True.
Step 2: Check 'world' palindrome status
'world' reversed is 'dlrow', which is different, so is_palindrome('world') returns False.
Final Answer:
True\nFalse -> Option A
Quick Check:
'level' palindrome True, 'world' not [OK]
Hint: Check string equals reversed string for each input [OK]
Common Mistakes:
Assuming all strings are palindrome
Mixing output order
Confusing print output format
4. Find the error in this palindrome check function:
def check_palindrome(s):
for i in range(len(s)):
if s[i] != s[len(s)-i]:
return False
return True
medium
A. Function returns True too early
B. Loop should run only half the string length
C. Index out of range error in s[len(s)-i]
D. No error, function is correct
Solution
Step 1: Analyze index usage in loop
Index len(s)-i is out of range because string indices go from 0 to len(s)-1.
Step 2: Identify correct index for reverse comparison
It should be len(s)-i-1 to avoid index error.
Final Answer:
Index out of range error in s[len(s)-i] -> Option C
Quick Check:
Check index bounds carefully [OK]
Hint: Remember last index is len(s)-1 [OK]
Common Mistakes:
Off-by-one error in indexing
Looping full length instead of half
Ignoring index errors
5. You want to check if a phrase is a palindrome ignoring spaces and capitalization. Which code snippet correctly implements this?
hard
A. s_clean = ''.join(c.lower() for c in s if c.isalpha())\nreturn s_clean == s_clean[::-1]
B. s_clean = s.replace(' ', '')\nreturn s_clean == s_clean[::-1]
C. s_clean = s.lower()\nreturn s_clean == s_clean[::-1]
D. s_clean = ''.join(c for c in s if c.isdigit())\nreturn s_clean == s_clean[::-1]
Solution
Step 1: Understand requirements
We must ignore spaces and capitalization, and consider only letters.
Step 2: Analyze each option
s_clean = ''.join(c.lower() for c in s if c.isalpha())\nreturn s_clean == s_clean[::-1] removes non-letters and lowercases all characters, then checks palindrome. s_clean = s.replace(' ', '')\nreturn s_clean == s_clean[::-1] only removes spaces but keeps punctuation and case. s_clean = s.lower()\nreturn s_clean == s_clean[::-1] keeps punctuation and spaces. s_clean = ''.join(c for c in s if c.isdigit())\nreturn s_clean == s_clean[::-1] keeps digits only, ignoring letters.
Final Answer:
s_clean = ''.join(c.lower() for c in s if c.isalpha())\nreturn s_clean == s_clean[::-1] -> Option A
Quick Check:
Filter letters and lowercase before palindrome check [OK]
Hint: Filter letters and lowercase before checking [OK]
Common Mistakes:
Only removing spaces
Ignoring punctuation
Not lowercasing letters
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.