Languages
[Edit]
EN

Python - reverse a string

0 points
Created by:
Teilsa
1255

In this article, we would like to show you how to reverse a string in Python.

Quick solution:

reversed_string = string[::-1]

Where the statement [::-1] means:

  1. start at the end of the string,
  2. end at position 0,
  3. move with the step -1 (step backwards)

 

Practical example

In this example, we use a slice that steps backwards to reverse the string.

string = "ABC"
reversed_string = string[::-1]

print("Original string:", string)
print("Reversed string:", reversed_string)

Output:

Original string: ABC
Reversed string: CBA

Alternative titles

  1. Python - how to reverse a string
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.

Cross technology - reverse string

Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join