# Reverse a String in Python 3

Tyler (271)
0

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:

python
PRIMARY
61 guides
code
65 guides
1

## 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.

2

## 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:
In Python, comprehensions are a useful construct that allows us to create new sequences in a very concise way.
Tyler
In these interests: codepython
Slack has become one of the most important tools for communication in many companies, mine included.
Tyler
In these interests: slackpython
If you're familiar with Python's keyword-only arguments, then you've probably wondered why the same constraint doesn't exist for positional arguments. This changes with Python 3.
Tyler
In these interests: codepython
Specify a parallel filesystem cache for compiled bytecode
AKA the Walrus Operator
Learn how to use formatted string literals in Python
While this requirement may seem rare, learning how to nest defaultdicts properly can be extremely powerful and save you from bloated and confusing initialization code.
In Python, we often run into scenarios where we need to merge two or more lists. This is also called list concatenation, and Python provides multiple methods for accomplishing this.
Find and fix slow running Python unit tests
In Python, range is an immutable sequence type, meaning it's a class that generates a sequence of numbers that cannot be modified.
Posted in these interests:
python
61 guides 109 subscribers
PRIMARY
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.
code
65 guides 99 subscribers
Code is poetry -- one line at a time.
Discuss this guide:
Follow @howchoo and learn cool things!