Python Slice Operator Error and solution

If you remember in the first tutorial of Python Slice operator series I said that I do not recommend putting Python Slice operator and variable declaration in the same line. Therefore, today in this blog I’ll explain to you the reason behind that.

In this tutorial, you will learn what does Python Slice Operator returns as a result and why we should not put it along with the variable declaration in the same line.

There is a reason why I am doing the tutorial on this topic. often, I’ve seen so many beginners and seasoned programmers committing this mistake. So, I don’t want you to be like them. Also, having an in-depth knowledge of a function and in this case, an operator is always good for the future.

This tutorial will require a working knowledge of Python Slice operator. Therefore I highly suggest you to please check out the first two tutorials of this series. Here are their links—

  1. The introduction to Python Slice Operator
  2. How to reverse a string using Python Slice operator.

Now let’s begin with today’s Python Tutorial –

What does Python Slice Operator Return?

As you know, Python slice is a process of selecting a range of indexes in just one step. Thus it returns those selected ranges of indexes. In other words, Python Slice operator returns a string object as a result.

Why you should put Python Slice with Variable Declaration?

First, let me clarify that – putting Python Slice operator with variable declaration is neither syntactically nor logically wrong. Consequently, you won’t get any errors when putting both of them together. And, that’s exactly what the problem is!

Because there is no error thus you will never know the problem until the situation arises. Let’s see what those situations could be. Also, let’s understand what happens when we put both the variable declaration and the Python Slice operator together in the same statement.

DemoString = "Your Salary + Bonus is: "[0:11:]

Here I have an executable statement where I am declaring a variable “DemoString” and storing a string (“Your Salary + Bonus is:”) into it. Let’s call this string an “Original String”. Followed by that string I have placed the slice operator into the same line. This slice operator is extracting a sub-string from index 0 to index 11. Let’s call this extracted sub-string a “sliced string”.

Now let me tell you what happens in the background when we execute this statement.  

On execution, the interpreter will perform two actions. These actions are –

Action 1: Interpreter will declare the variable “DemoString” and initialize it by storing the Original string into it.

Action 2: It will process the Python Slice operator and extract the sub-string from index 0 to index 11. After extracting the “Sliced String” it will store that string into the same variable “DemoString” which it initialized with the “Original String” earlier.

Thereafter action 2 the value stored into the variable “DemoString” will not be the “Original String” rather it will be the “Sliced String”.

You got it right. After processing the slice operator the interpreter will replace the “Original Value” with the “Sliced Value”.

In simple words after “Action 1” the value which is stored into the variable “DemoString” will be the Original String – “Your Salary + Bonus is:”. Whereas after “Action 2” the value stored into the variable “DemoString” will be the Sliced String – “Your Salary”.

How does it matter?

So, now you must be wondering, how does it matter? Let me explain.

Let’s say I want to write a program which will accept the salary from the user. Furthermore, on execution, it must show the current salary of the user and the salary after adding a $1000 bonus back to the user with formatted string. Like this

Example of Python Slice operator

If you will analyze both these print statements carefully then you will notice that both share the same initial string “Your Salary”. Wouldn’t it be awesome if we can device an option where we don’t have to write this string repeatedly?

That option is to save this string into a variable and use that variable in the program wherever we want. Something like this

DemoString = "Your Salary + Bonus is: "
salary = int(input("Please Enter Your Salary: "))
print(DemoString, salary)
Bonus = salary+1000
print("Your Salary + Bonus is: ", Bonus)

There is a problem with the above program. That is – the output is not as per our desire.

In the first print statement, we only needed the first part of the string which is “Your Salary”. Whereas in the second print statement we required the entire string which is stored into the variable. But in the output, both the print statements are printing the entire string stored into the variable

wrong output of slice operator of python programming

Problem in putting slice operator and variable declaration together.

This problem can easily be solved using slice operator. But we must be very careful with that. In order to solve such a problem, most of the time what we do is – we put Python Slice operator with the variable declaration. Without knowing its consequences.

Let’s create those adverse situations to understand what could go wrong if we put Python Slice Operator and variable declaration in the same line.

DemoString = "Your Salary + Bonus is: " [ 0 : 11 : ]
salary = int(input("Please Enter Your Salary: "))
print(DemoString, salary)
Bonus = salary+1000
print(DemoString, Bonus)

Now if you will execute this program then you will see that both the print statements will print the sliced string (“Your Salary”) in the output. Because after processing the slice operator the interpreter has replaced the “original String” with the “sliced string”.

sliced output of slice operator of python programming

How to solve this problem?

This problem is arising because you are putting the slice operator with the variable declaration. Therefore on processing the statement, the interpreter is replacing the “Original String” with the one which is returned by the slice operator.

So, the solution to this problem is to use the slice operator only when it is required. For example, here in this code, we needed python slice operator only with our first print statement. Except that we don’t need to use it anywhere else.

So, instead of putting the slice operator with the variable declaration we will put it right here in this print statement. That should solve our problem.

You can press the play button to watch how to implement the mentioned solution. I ensure you that you won’t regret it –

That’s it for this tutorial. If you have any question, feel free to message me on my Facebook. Hope you enjoyed reading. Do make sure to share the link on your Facebook.

Thanks and have a great day!