Zed-King Institute

    Python Program to Check Palindrome String

    mediumStringsPython

    A palindrome reads the same backwards — madam, level, radar. Python's slicing makes the check one line.

    The question

    Read a word and print whether it is a palindrome (reads the same backwards). Ignore upper/lower case, so Madam is a palindrome.

    The code

    program.py

    w = input().strip().lower()
    if w == w[::-1]:
        print("It is a palindrome")
    else:
        print("It is not a palindrome")

    Input

    Noon

    Output

    It is a palindrome

    How it works

    1. 1.strip().lower() removes spaces around the word and ignores capital letters.
    2. 2w[::-1] is the string reversed (a slice with step −1).
    3. 3If the word equals its reverse, it is a palindrome.

    Common mistakes

    • Not using .lower() — Madam then fails because M and m are different characters.
    • Comparing with is instead of == — is checks identity, not equal text.

    Now solve a similar question yourself

    A new Python question every time, checked instantly.

    Practice questions

    More in Strings