0
0
PythonHow-ToBeginner · 3 min read

How to Reverse a String in Python: Simple Syntax and Examples

You can reverse a string in Python using string[::-1], which uses slicing to step through the string backwards. This is a simple and efficient way to get the reversed version of any string.
📐

Syntax

The syntax to reverse a string uses slicing with three parts: string[start:stop:step]. To reverse, you leave start and stop empty and set step to -1, which means move backwards through the string.

  • start: where to start (default is the end when stepping backwards)
  • stop: where to stop (default is before the start)
  • step: how many steps to move each time (negative means backwards)
python
reversed_string = original_string[::-1]
💻

Example

This example shows how to reverse the string "hello" using slicing. It prints the reversed string.

python
original_string = "hello"
reversed_string = original_string[::-1]
print(reversed_string)
Output
olleh
⚠️

Common Pitfalls

One common mistake is trying to reverse a string using a method like reverse(), which does not exist for strings in Python. Another is misunderstanding slicing syntax and using positive step values, which won't reverse the string.

Strings are immutable, so methods that modify in place (like list methods) do not apply.

python
wrong = "hello"
# This will cause an error because strings have no reverse() method
# wrong.reverse()  # AttributeError

# Correct way:
correct = wrong[::-1]
print(correct)
Output
olleh
📊

Quick Reference

Use slicing with string[::-1] to reverse strings quickly and easily in Python.

  • Works on any string
  • Does not change the original string
  • Returns a new reversed string

Key Takeaways

Use slicing with string[::-1] to reverse strings in Python.
Strings are immutable, so reversing returns a new string without changing the original.
Do not use reverse() method on strings; it does not exist.
Slicing syntax is string[start:stop:step], and a negative step reverses the string.