reverse string in ;ython

How to Reverse String in Python

Python is a powerful programming language that allows you to perform various operations on strings. However, it does not contain an inbuilt function to reverse a given string. In this article, we will look at different ways to reverse string in Python.


How to Reverse String in Python

Here are the different ways to reverse string in python.


1. Slicing

Every string in python can be treated as an array of characters such that the first character has an index 0, the second one has an index 1 and so on. Python also provides a negative index whereby the last character is assigned index -1 the one before has an index of -2 and so on. You can use positive or negative index for each character, depending on your requirement.

Using this, we will reverse a string. Here is the syntax to reverse string using slicing.

string[start:stop:step]

In the above command, the start value is the index at which extraction should begin. If you omit this value or keep it blank, then reversal will start from index 0. If you use a negative index, it will be interpreted as an offset from last character.

The stop value is the index before which reversal must stop. If it is negative, it is seen as an offset from last character. If it is greater than string length, then reversal will happen till last character and stop.

The step value is the step of slicing. If you leave it blank then the default step of 1 is used. If you use -1 then python will slice elements in reverse order.

So to reverse a string, you need to use the following command. In this case, we omit start & stop values and use -1 as step value. Python starts slicing from last element and works in reverse order.

string[::-1]

Here is an example.

>>> a='hello'
>>> a[::-1]
'olleh'


2. Using reversed

Python provides a reversed function that processed string characters in reverse order and returns an iterator. Here is an example to use the reversed iterator’s elements to an empty string.

>>> a='hello'
>>> "".join(reversed(a))
'olleh'


3. Using List reversal

You may also convert a string to a list first and then use reverse function readily available to reverse python lists. Then you can convert the list back to string.

>>> a='hello'
>>> l=list(a)
>>> l.reverse()
>>> ''.join(l)
'olleh'

In this article, we have learnt 3 different ways to reverse string in python. Among them, the first method using slicing is the fastest method. However, it consumes most memory so it can be inefficient with large strings. On the other hand, the second method consumes least memory since it returns an iterator. So it can be used with large strings. But it is slower than slicing. You may use either of these methods depending on your requirement. If you are working with small to medium strings and without memory constraints, then us slicing. If you work with large strings and have limited memory available, use second method using reversed.

Also read:

How to Connect to PostgreSQL using Python
How to Remove Snap in Ubuntu
How to Select Random Records in MySQL
How to Recursively Change Directory Owner In Linux
How to Append Text At End of Each Line in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *