Array of Palindrome Strings

Check which strings in array are palindromes.

Logic BuildingIntermediate
Logic Building
# Take array of strings
n = int(input("Enter number of strings: "))
arr = []
for i in range(n):
    arr.append(input(f"String {i+1}: "))

# Find palindromes
palindromes = []
for s in arr:
    if s == s[::-1]:
        palindromes.append(s)

print(f"Palindromes: {palindromes}")

Output

Enter number of strings: 4
String 1: hello
String 2: racecar
String 3: world
String 4: level
Palindromes: ['racecar', 'level']

Check each string for palindrome property.

Key Concepts:

  • Compare string with reverse
  • Add to list if palindrome
  • Filter palindromes