HOWTO · Python

How to Check if a Python String Is a Palindrome

Check whether a Python string is a palindrome with slicing, reversed(), loops, recursion, deque, and text normalization.

On this page

A palindrome reads the same forwards and backwards, such as level. The basic check compares every character as entered. Normalize the text first when the comparison should ignore case, spaces, or punctuation.

Check Whether a Python String Is a Palindrome with Slicing

String slicing creates a reversed copy. The [::-1] slice visits a string from its final character to its first, so comparing it with the original is a compact raw check. Replace level with the value you want to check.

word = "level"
print("Palindrome" if word == word[::-1] else "Not Palindrome")

Slicing works directly on strings; it is not a list method. This comparison is case-sensitive and treats spaces and punctuation as ordinary characters. It takes linear time and creates a reversed string.

Check Whether a Python String Is a Palindrome with reversed()

The built-in reversed() function returns an iterator over the characters in reverse order. Use join() to turn that iterator into a string before comparing it with the original.

word = "level"
reverse = "".join(reversed(word))
print(word == reverse)

reversed() itself does not return a string. Joining and comparing visit the characters, so this approach takes linear time; unlike slicing, it does not first create a reversed copy through a slice.

Check Whether a Python String Is a Palindrome with a Loop

A loop can compare each character with its partner at the opposite end. The index on the right is len(word) - index - 1; checking only half the positions covers every pair.

word = "radar"
result = True
for index in range(len(word) // 2):
    if word[index] != word[len(word) - index - 1]:
        result = False
        break
print(result)

The loop stops as soon as a pair differs. Its work is linear in the length of the string, and it avoids building a reversed copy.

Check Whether a Python String Is a Palindrome with Recursion

Recursion compares the outside characters, then calls the same function with the substring between them. A string with fewer than two characters is the base case: there are no differing outer characters to find.

def is_palindrome(text):
    if len(text) < 2:
        return True
    return text[0] == text[-1] and is_palindrome(text[1:-1])

print(is_palindrome("level"))

This version is useful for learning how a problem can be reduced into smaller checks. Each call slices a substring, which adds copying work, and long inputs can exceed Python’s recursion limit; use the slicing or loop approach for ordinary application code.

Check Whether a Python String Is a Palindrome with a deque

A collections.deque is a double-ended queue. It lets the check remove one character from each end and compare the pair. Constructing the deque and checking all pairs take linear time overall.

from collections import deque

def is_palindrome(text):
    chars = deque(text)
    while len(chars) > 1:
        if chars.popleft() != chars.pop():
            return False
    return True

print(is_palindrome("radar"))

The individual end removals are constant-time operations, but the full check is not constant time: it may process every character. This method compares the raw characters, including case, spaces, and punctuation.

Ignore Case and Non-Alphanumeric Characters When Checking a Palindrome

For phrase-style checks, first lowercase the text and keep only alphanumeric characters. This policy changes what counts as a palindrome: case, spaces, and punctuation no longer affect the comparison, while letters and digits remain significant.

def is_palindrome(text):
    normalized = "".join(char.lower() for char in text if char.isalnum())
    return normalized == normalized[::-1]

phrase = "A man, a plan, a canal, Panama!"
print(is_palindrome(phrase))

isalnum() retains letters and digits. Python’s string methods support Unicode characters; if an application accepts only a particular alphabet, define that character policy explicitly instead.

Choose a Python Palindrome-Checking Approach

Use word == word[::-1] for the shortest raw check. Choose reversed() with join() when an iterator-based alternative is useful, or a loop when explicit pairwise comparisons and early exit make the logic clearer. A deque is another way to compare both ends. Recursion is mainly instructional because each call slices text and long inputs can reach the recursion limit.

Decide whether to compare the input literally or normalize it first; that choice determines how case, spaces, and punctuation affect the result. For related text-processing techniques, see Python regular expressions (regex).