Array of Palindrome Strings

Check which strings in array are palindromes.

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']

What's going on

Check each string for palindrome property.

Key Concepts:

Compare string with reverse
Add to list if palindrome
Filter palindromes