Practise the basic operations on strings: length, reverse and palindromes.
What to build
A program that reads one word and reports its length, its reverse and whether it is a palindrome.
Requirements
- Print
length N, thenreverse R, thenpalindrome yesorpalindrome no. - Print the reverse of the empty word as ε.
Input
One line: a word over {a, b}; the line may be empty, meaning ε.
Output
Three lines, as described.
Constraints
The word has at most 50 symbols.
Example
Input:
abba
Output:
length 4
reverse abba
palindrome yes
Why it works
Reverse and length are the first operations on strings; a palindrome is a word that equals its own reverse.
Examples
Example 1 · Input
abba
Expected output
length 4 reverse abba palindrome yes
Example 2 · Input
aab
Expected output
length 3 reverse baa palindrome no
Prompts inside input() are ignored when checking, so input("n: ") is fine.
Hints
Hint 1
len(word) gives the length and word[::-1] the reverse.
Hint 2
A word is a palindrome when it equals its reverse; ε is a palindrome.
Solution
Show one correct solution (try it yourself first)
word = input()
print("length", len(word))
print("reverse", word[::-1] or "ε")
print("palindrome", "yes" if word == word[::-1] else "no")