← All examples

Palindrome

Python Algorithms

string palindrome

Flowchart (ISO 5807)

YesYesNoNoStartInput si, j = 0, len(s) - 1i < js[i] != s[j]Return FalseEndi += 1j -= 1Return TrueEndFigure 1 — is_palindrome

Source code

def is_palindrome(s):
    i, j = 0, len(s) - 1
    while i < j:
        if s[i] != s[j]:
            return False
        i += 1
        j -= 1
    return True