Array of Palindrome Strings
Filter palindrome strings from a Python list. Students compare exact reversals and preserve repeated matches while keeping case and whitespace significant.
1# Take array of strings2n = int(input("Enter number of strings: "))3arr = []4for i in range(n):5 arr.append(input(f"String {i+1}: "))67# Find palindromes8palindromes = []9for s in arr:10 if s == s[::-1]:11 palindromes.append(s)1213print(f"Palindromes: {palindromes}")
Output
Enter number of strings: 4 String 1: hello String 2: racecar String 3: world String 4: level Palindromes: ['racecar', 'level']
In this one
- Filter exact palindromes
- Keep repeated matches
- Preserve case and whitespace
How it works
- 1
Visit each stored string.
- 2
Compare s with s[::-1].
- 3
Append matches unchanged to palindromes.
Edge cases
Does an empty stored string qualify?
Does an empty stored string qualify?
Yes. Its reversal is also empty.
Are spaces ignored?
Are spaces ignored?
No. They participate in the exact comparison.
What's going on
Each s is checked with s == s[::-1]. Matching strings are appended unchanged to a separate palindromes list.
The comparison is case-sensitive and includes every character. level passes, but Level fails. Empty strings and one-character strings pass because they equal their reverses. Repeated qualifying strings remain repeated; this is a filter, not a deduplication step.
Does an empty stored string qualify? Yes. Its reversal is also empty.
Are spaces ignored? No. They participate in the exact comparison.
Quiz
Tap an option. The answer stays hidden until you pick.
Question 1
Is Level accepted?
Pick an answer to check it.
Question 2
Does an empty stored string qualify?
Pick an answer to check it.
Questions people ask
Does an empty stored string qualify?
Yes. Its reversal is also empty.
Are spaces ignored?
No. They participate in the exact comparison.
Is Level accepted?
No.
๐Related Content
- ๐
Phase 6 - Learn Concepts
Review Phase 6 concepts and explanations
- ๐
Phase 6 - Quiz
Test your Phase 6 understanding with quiz questions
- ๐ป
Phase 6 - All Practice Problems
Explore all practice problems for Phase 6
- ๐
Master Your Logic Building - Complete Course
Browse all phases and tutorials
- ๐ง
Logic Building Overview
Learn about the complete logic building curriculum