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 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.
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.
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 ” to ‘loohcsyrrebedoc’.
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
- How to use the Python for loop
- How to use Python Sets
- How to use a Python Dictionary
- How to use Python Classes
- How to use Python Range
- How to use Python if-else statements
- How to use Python RegEx
- How to use Python Lists
- How to use Python Enumerate
- How to use Python Functions
- How to use Python Split
- How to use Python Try-Except
- How to use Python Tuples
- How to use Python Arrays
- How to use Python Sort
- How to use the Python DateTime
- How to download Python?
- How to use the Python FileWrite function
- How to use Python Lambda
- How to use Python ListAppend
- How to use Python ListComprehension
- How to use Python Map
- How to use Python Operators
- How to use Python Pandas
- How to use Python Requests
- How to use Python Strings
- How to use Python Count
- How to use Python Comments
- How to use the Python File Reader method
- How to use the Python IDE-s
- How to use Python logging
- How to use Python Print
- How to use the Python Zip
- How to use Python Append
- How to use Python Global Variables
- How to use the Python join method
- How to use Python list length
- How to use Python JSON files
- How to use Python Modulo
- How to use Python file opening methods
- How to use Python round
- How to use Python sleep
- How to use Python replace
- How to use Python strip
- How to use the Python Time module
- How to use Python unittests
- How to save data to a text file using Context Manager?
- How to use Python external modules
- How to use Python find
- How to install the Python pip package manager
- How to delete files in Python
- Parsing XML files in Python
- How to make a GUI in Python
- How to use Python in Command Prompt
- How to Run a Python Program in VS Code
- How to run a program in Python IDLE
- How to run a program in Jupyter Notebook
- How to read a text file in Python
- How to add numbers in Python
- How to ask for user input in Python
- How to debug in Python
- How to create a thread in Python
- How to end a program in Python
- How to import a library in Python
- How to use the PIP package manager
- How to use classes in Python
- How to reverse strings in Python
- How to convert a string to int in Python
- How to print on the same line in Python
- How to remove items from a list
- How to add to a dictionary in Python
- How to raise an exception in Python
- How to throw an exception in Python
- How to stop a program in Python
- How to use Python assert
- How to use the Python compiler
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.
