Join us and get access to thousands of tutorials and a community of expert Pythonistas.
This lesson is for members only.Join us and get access to thousands of tutorials and a community of expert Pythonistas.
Reversing a String Using a Custom Algorithm
00:00In this lesson,you will learn to reverse a string by writing your very own string reversalalgorithm.Note that the algorithm described in this lesson is only one of many possibleapproaches. Before diving in, you’ll need a few tools to accomplish this task.
00:16Python has a neat language feature referred to as tuple unpacking.It can be used to assign the elements of a tuple to multiple variables in asingle statement. It can also be used to swap variables.
00:28Let’s assign the values of1 and2 to the variablesa andb.The variablesb anda can be packed into a tuple and then unpacked in the samestatements into the variablesa andb. This effectively swaps their values.
00:46One potential algorithm for reversing a string is to incrementally swap the leftand right characters of a given string.This type of approach is usually referred to as a two-pointer solution.
00:57In this case, the two pointers would be a left pointer and a right pointer.These pointers are integers which point to the elements to be swapped.Let’s go through an iteration of the string reversal algorithm.
01:09If you want to have a closer look at the image you see on this slide,you can find it in high resolution in the materials for this course.In the first step,the pointer would start at the zeroth index, pointing to the first character,and the right pointer would start at the end, pointing to the last character.
01:26The characters at those locations are then swapped.The left pointer is then incremented by one, with the right pointer beingdecremented by one.This is done repeatedly until the left and right pointers meet, at which pointall characters have been swapped. For example,let’s say you want to reverse the string"HELLO". During the first iteration,the"H" and"O" would be swapped, as the left pointer is equal to0,and the right pointer is equal to4. During the second iteration,the left pointer has now been incremented to1, with the right having beendecremented to3. Therefore,the"E" and the"L" would be swapped. During the third and finaliteration,the left and right pointers would both equal2, and therefore the algorithmwould exit, returning the string,"OLLEH".
02:15Let’s implement this algorithm in a function calledreverse_string().
02:20It takes in a single argument,a string, and returns that string with the characters in reverse order.During each iteration of the algorithm,the characters pointed at by the left and right pointers are swapped. However,strings are immutable, meaning that they cannot be changed. Therefore,it is better to work with a mutable sequence that will store our characters, suchas a list. The swap operations can then be performed on this list,and once the algorithm exits, the list can be joined back into a string.
02:51Next,let’s initialize the left and right pointers to point to the start and the endof our list, respectively.
02:59Recall that the string reverse algorithm runs until the left and right pointersare equal.A common way to do this is with a conditionalwhile loop, with the conditionbeing that the left pointer must be lesser than the right.
03:12If that condition check fails, the loop will exit. Inside the loop,you can use the tuple packing trick you learned earlier to swap the charactersat the location pointed at by the left and right pointers.
03:26Finally,the left and right pointers should be incremented and decremented, respectively.
03:34Once thewhile loop is exited,all the characters should be in the correct order within your list..join() can be used to combine this list back into a string,and this can be directly returned.
03:51Let’s test this out on the string"Hello" from before.
03:56Great.By leveraging Python features such as tuple unpacking and using a two-pointerapproach, you can implement your very own custom string reversal algorithm.
04:06Writing a custom algorithm is not usually recommended when preexisting solutionsexist. It is, however,an important part of the learning process and plays a significant role intechnical interviews.
04:19Now that you’ve learned quite a bit about how to reverse the string,let’s recap the content of this course in the last lesson.
Course Contents

