Skip to content

Palindrome

Python string palindrome

Flowchart (ISO 5807)

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

Source code

Python
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

The editor opens with this code — change it and the chart redraws.

Language
Python
Constructs
string palindrome
Topic
Algorithms
Formats
Word, Visio, draw.io, PDF, SVG

Related examples