How to reverse a string in Python – with example

  • Reading time:18 mins read

How to reverse a string in Python and when to use it

Strings are immutable objects in Python, which means that even if we wanted to reverse them in place, we will encounter an error.

However, Python provides other optional methods that we can use to reverse strings such as slicing and the inbuilt method reversed().

Since strings can be indexed, sliced and we can also iterate through them this means we may combine various methods when reversing.

While this seems like a basic skill, learning how to manipulate strings in Python or any other programming language is an essential skill that will be handy when working with different kinds of data. 

 

 

We will begin by creating a string called mystr that will initialize with the string “codeberryschool”.

How to reverse a string in Python – Using the type() method

Now using the type() method which returns the class object of the object passed as an argument in the parentheses,  we can verify that indeed this is a string object as shown below.  

 

 

How to reverse a string in Python through slicing

Now slicing is a technique that allows us to extract elements from sequences such as strings by specifying their indices with square brackets.

For example, from the mystr string, we can extract elements starting from the first index up to the third index as shown below.

 

 

Since Python is a zero-based index language the element in the first index is, therefore “o” and not “c”. 

In addition to this, the element at index 3 is not included since in Python we normally slice up to but not include the element on the last index.  

 

How to reverse a string in Python

 

How to reverse a string in Python by using indexing

In as much as we can index elements from the start of the string, we can as well slice elements from the end of the string using negative indexing.

In the example below we are asking the interpreter to include elements from the start of the string up to the third element from the end of the string.

It is important to recall that when counting elements from the end of a string we begin with -1 as the last element and not 0.

 

 

We can equally achieve the result above by slicing off the last three elements of the string mystr leaving the rest of the string intact as shown below.

 

 

When slicing we can also include a third optional integer that specifies the number of elements that we want to periodically jump when slicing. This is known as the step integer.

How to reverse a string in Python by using the “step” integer

The first and second indices are known as the start and stop indices that indicate where in the string to start slicing and where we should stop.  

 

mystr[start:stop:stop]

 

The start, stop and step are optional when slicing elements from a string. When the start and stop are not specified the defaults are used as follows: the start index will be 0(Zero) while the stop index will be the length of the string.

This, therefore, means that if we don’t specify both the start and stop indices we will have the whole string returned.   

 

 

Now using these default offsets we can reverse the string by specifying the stop number as -1.

How to reverse a string in Python by using negative indexing

Using the step as a negative number means that slicing will now begin from the end of the string to the beginning, i.e from the right to the left.

 

How to reverse a string in Python

 

This returns the copy of the string object with the letters reversed as shown below.  

 

 

How to reverse a string in Python by using the reversed method

We can also use the reversed method together with the .join() method to reverse any string in Python.

When a string is passed into the reversed() method it returns an iterator object containing the characters in reverse order.  

 

 

Now using the next() method we can access the next elements in this iterator object as shown below.

 

 

The join() method takes items from an iterator and joins them together to form a string. Using this method we can then join these characters to form the string that is in reverse order.

 

How to reverse a string in Python

 

In this case, we have not specified any separator therefore the character will be together as shown below.

 

 

As shown in the output above we have successfully managed to reverse the string “codeberryschool ”  toloohcsyrrebedoc’.

Summary

If you’d like to see more programming tutorials, check out our Youtube channel, where we have plenty of Python video tutorials in English.

In our Python Programming Tutorials series, you’ll find useful materials which will help you improve your programming skills and speed up the learning process.

Programming tutorials

Would you like to learn how to code, online? Come and try our first 25 lessons for free at the CodeBerry Programming School.

Learn to code and change your career!

100% ONLINE

IDEAL FOR BEGINNERS

SUPPORTIVE COMMUNITY

SELF-PACED LEARNING

Not sure if programming is for you? With CodeBerry you’ll like it.