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

Check Palindrome String

Check exact text palindromes with Python string reversal. Students trace equality and see how case, spaces, and punctuation can change the final decision.

BeginnerPhase 5 - Category 1: Basic String HandlingExample 8 of 10
check-palindrome-string.py
1# Take string input
2s = input("Enter a string: ")
3
4# Check palindrome
5if s == s[::-1]:
6 print("Palindrome")
7else:
8 print("Not a palindrome")

Output

Enter a string: racecar
Palindrome

Enter a string: hello
Not a palindrome

In this one

  • Compare exact sequences
  • Use a reversed copy
  • Retain case and punctuation

How it works

  1. 1

    Read s unchanged.

  2. 2

    Compare it with s[::-1].

  3. 3

    Print the matching palindrome label.

Edge cases

Does empty input pass?

Does empty input pass?

Yes. It equals its own reverse.

Does Level pass?

Does Level pass?

No. The comparison is case-sensitive.

What's going on

The comparison uses s exactly as entered and the new reversed string s[::-1]. No normalization happens before equality.

level passes, while Level fails because uppercase L differs from lowercase l. Spaces and punctuation also participate. Empty and single-character strings pass because their reverse is identical; this is a direct sequence comparison rather than a phrase-cleaning algorithm.

Does empty input pass? Yes. It equals its own reverse.

Does Level pass? No. The comparison is case-sensitive.

Quiz

Tap an option. The answer stays hidden until you pick.

Question 1

What does the code ignore?

Pick an answer to check it.

Question 2

Does empty input pass?

Pick an answer to check it.

Questions people ask

Does empty input pass?

Yes. It equals its own reverse.

Does Level pass?

No. The comparison is case-sensitive.

What does the code ignore?

Nothing in the entered text.