How To Reverse A String Using Slice Operator on Python Programming

Though I have given you the idea of how to reverse a string using Python Slice operator in the last tutorial yet here I’ll explain the concept in detail so that you can face the interview with confidence.

Suggested Reading

This tutorial will require a working knowledge of Python Slice operator. Thus I highly suggest you check out the last tutorial before going ahead with this one.
Introduction to Python Slice Operator

If you have ever search for how to reverse a string in Python, then you must have gotten the answers similar to this one –

Program 1:

str = “Rebellion Rider” [ : : -1]
Print(str)

Such answers are all over the internet but there are very few instances where someone has actually tried to explain it. I have seen that the lack of explanation has left many of you guys in confusion. Therefore, I’m going to explain how to reverse a string in Python using slice operator in detail right here, right now. So, you can face your interview with confidence & not confusion.

What Google is hiding about Python Slice?

To understand the concept let’s write one more program and then we will compare that one with the above code (program 1)–

Program 2:

str = “Rebellion Rider” [ : : 1]
Print(str)

Both Program 1 and Program 2 are pretty much the same except for one thing which is the value for the last flag steps of the slice operator. In program 1, the value for the flag steps is -1 whereas the value for flag steps in program 2 is 1

On execution Program 1 will print the string in reverse order while program 2 will print the string as it is, stored in the variable.

This clearly shows that the value for flag steps 1 and -1 has some sort of special interpretation. That special interpretation is not only changing the output but also changing the behaviour of the execution of Python Slice operator.

What is the Mystery behind +ve and -ve numbers?

The third flag of Python Slice operator which is “Steps” tells the interpreter the interval of the elements to be extracted. For example, if I have specified 2 then every second element of the string will be extracted.

It’s not the number it’s the polarity!

It is not about +1 or -1. It’s about the polarity of the number. The number could be anything +2, -4 or something else. It’s about the sign carried by the number which is changing the behaviour of the execution of Slice operator of Python Programming.

A positive number tells the interpreter to process the string from Left-To-Right. Which means +2 specified as the value for the third flag of slice operator will tell the interpreter to extract every second element of the string from Left-To-Right.

Polarity of Python Slice Operator

Whereas, a negative number tells the interpreter to process the string from Right-To-Left. Which means -3 specified as the value for the third flag of slice operator will tell the interpreter to extract every third element of the string from Right-To-Left.

What’s so special about 1?

Yes, 1 plays a vital role in printing a string in reverse order. Let me tell you how. When 1 is supplied as the value for the flag “steps” of the slice operator, then it tells the interpreter to extract every single element in consecutive order.

For example, in Program 2 the value for the “steps” is a positive 1 which means the interpreter will extract all the elements of the string in the same order as they were stored in the variable ‘str’.

String processed from left by Python Slice Operator

Whereas, in program 1 we have assigned a negative 1 (-1) for the ‘steps’. Here also the interpreter will extract all the characters of the string consecutively. But, it will start the extraction from the end of the string.      

String of negative polarity processed by Python Slice Operator

So we can say that – a positive or negative number can change the entire flow of interpretation when used as the value for the third flag of the Python Slice operator. Because it not only tells the interpreter to extract every single element but also the direction of processing a sequence like string.

You can watch this video as here I have explained the concept in detail with animation.

Things Which You Won’t Find On The Internet About Python Slice

In this Python tutorial, I have explained to you all the details of how to reverse a string using Python Slice operator. Now you are all set to nail your interview. In case if you still have any question then feel free to leave me a message on my Facebook or Twitter

Please share this blog on your social media. Thanks and have a great day.