Ad space (banner)
🐍Python Lessons
Lesson 44 / 69

Reversing a String and Checking for Palindromes

This lesson covers reversing a string and checking for palindromes using slice notation, so you can pick up Python's flexible string-manipulation style. It's written for anyone searching "Python reverse string" or "Python slicing tutorial".

The slice notation [::-1] reverses a string. A string that reads the same forwards and backwards is called a palindrome. Python's slice notation is a powerful feature for flexibly extracting part of a string or list, and [::-1] — "the whole thing, reversed" — is one of its most classic uses.

The sample code's is_palindrome function compares the original string against its reversed version, s == s[::-1], in a single line. What often takes several lines in other languages, Python's slice notation lets you write astonishingly concisely.

A common beginner mistake is understanding the meaning of the three values in slice notation — start, stop, and step. [::-1] omits both start and stop, and sets the step to -1, meaning "one at a time, from the end toward the beginning." Memorizing this pattern makes other slices much easier to understand too.

The same idea applies directly to reversing a list — arr[::-1] reverses a list's elements. It's also used for small string puzzles and game logic, and it's a classic exercise found in many learning materials for practicing string manipulation.

Python
OUTPUT

💡 The Python engine may take a few seconds to load the first time you run code.

Ad space (banner)
Ad space (in-article)