Founding Batch Offer ยท HLD ยท LLD ยท System Design at โ‚น799 until 30 Nov

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.

IntermediatePhase 6 - Category 2: Array & String CombinationsExample 5 of 10
array-of-palindrome-strings.py
1# Take array of strings
2n = int(input("Enter number of strings: "))
3arr = []
4for i in range(n):
5 arr.append(input(f"String {i+1}: "))
6
7# Find palindromes
8palindromes = []
9for s in arr:
10 if s == s[::-1]:
11 palindromes.append(s)
12
13print(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. 1

    Visit each stored string.

  2. 2

    Compare s with s[::-1].

  3. 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.