# How to Reverse a String in Python 3

Tyler (291)
Total time: 2 minutes

There are multiple ways to reverse a string using Python 3. In this guide we're going to cover the extended slice operator and the reverse function. While they accomplish approximately the same thing, they each have a slightly nuanced use.

Posted in these interests:
h/python67 guides
h/code68 guides

## Reverse the string using the extended slice operator

Assuming we have the following string:

``> x = "hello world"``

we can reverse it like this:

``````> x[::-1]
'dlrow olleh'``````

### The extended slice operator explained

The extended slice specifies a beginning, end, and step. Beginning is the place to begin in the sequence. End is the place to stop in the sequence. This allows you to slice off a portion of a sequence.

For the string

``x = "hello world"``

We can get the 2 - 5 characters like this:

``x[2:5]``

Notice that this starts after the second character as the index starts at 0, and it ends after the fifth character producing:

``"llo"``

To match our example, leaving the begining and end values empty means that we start at the beginning of the sequence and end at the end. Basically it includes the entire sequence.

``"hello world"[::]``

produces:

``"hello world"``

Step refers to how we will step through the sequence. If step is -1 then we are stepping through the sequence in reverse hitting every element. If step is 5, then we are moving through the sequence hitting every fifth element.

So using this:

``"hello world"[::-1]``

means that we include the entire string and step through backwards ultimately reversing the string.

## Reverse a string using `join` and the `reversed` function

The `reversed` function is a built-in function that takes an iterable and returns a "reversed" iterator.

Again, let's take our "hello world" string as an example:

``> x = 'hello world'``

We can reverse it like so:

``````> ''.join(reversed(x))
'dlrow olleh'``````

### The `reversed` function explained

The `reversed` function takes an iterable and returns a reverse iterator. If you simply called `reversed` on a string, you'll see the iterator object:

``````> reversed('hello world')
<reversed at 0x10f583400>``````

This behaves like a normal iterator, so you can cast it as a `list` or use it in a `for` loop or use `join` to turn it into a string.

Tyler
Joined in 2015
Software Engineer and creator of howchoo.
Related to this guide:
Get the latest edition of Python in just minutes.
In these interests: codewindowspython
Run Python scripts in command prompt without typing the whole path.
In these interests: windowscodepython
You can run any Python script in a command-line interface.
In these interests: python
Got a Python question? We've probably answered it here.
Not sure what version of Python you’re running? Time to find out!
Specify a parallel filesystem cache for compiled bytecode
AKA the Walrus Operator
Learn how to use formatted string literals in Python
Posted in these interests:
h/python 67 guides
Python is howchoo's favorite programming language. We believe python promotes the most organized and performant codebase possible. We also love Django so, naturally, we love Python.
h/code 68 guides
Code is poetry -- one line at a time.
Discuss this guide:
We're hiring!
Are you a passionate writer? We want to hear from you!

Follow @howchoo and learn cool things:

Like what we do?

Want to support Howchoo? When you buy a tool or material through one of our Amazon links, we earn a small commission as an Amazon Associate.