What Is [:] or Slicing in Python?

[colon] In Python

Arrays and strings are the most commonly used data structures in Python. They are very powerful and act as the basis of programming in general. Array slicing or string slicing helps to enhance the capabilities of the arrays and the strings. They can be very useful if we know how and when to use them. But it feels a little complex at first glance. So, in this article, we will try to understand string/list slicing or, specifically, what is [:] in Python.

What is an array or string?

An array is a continuous and contagious series of data where each element is of a similar data type. Python has a successor of an array called a list. A list is similar to an array except it can have data of different data types.

A string is just an array of characters. Python also has huge functionality for strings just like lists. A lot of times in programming you need to get a substring or a subarray of a string or an array. For a situation like that, Python blesses us with slicing.

Related: Learn more about arrays or strings in Python.

What is slicing?

Slicing is the process of selecting a slice of string or an array from a bigger string or array. For example, we have the string “I love programming”, here “programming” is a slice of the string “I love programming”. Slicing can also similarly work on lists. For example, we have a list [10,20,30,40,50]. [10,20,30] is a slice of the list [10,20,30,40,50].

How to perform slicing in Python?

Slicing is an important part of list/string manipulation. Unlike other languages, python makes slicing easy with built-in slicing methods. Let’s understand slicing with the help of a simple example:-

Implementation of slicing in Python

my_list = [1,2,3,4,5] # list of numbers from 1 to 5
print(my_list) # printing the entire list
print(my_list[0:5]) #slicing the list from first element to last
print(my_list[1:]) #slicing the list from 2nd element to the last element
print(my_list[:4]) #slicing the list from first element to the 4th element
print(my_list[1:4]) # slicing the list from the 2nd element to the 4th element
Code And Output For Slicing In Python
Code And Output For Slicing In Python

In the above code block, a list of numbers is defined in my_list. First, we printed the list as it is using the print(my_list). Then, we started slicing the list. First, we sliced the entire list, i.e., the output will be the entire list itself. That is in the code print(my_list[0:5]) we start from 0 which is the first number of the list that is ‘1’, and we print all of the contents of the list after 1, which is ‘2,3,4’ along with the 5th element of the list that is ‘5’. Hence, the entire list comes into the purview of the slice.

The syntax for slicing is:

list_name[starting_index:ending_index:step]

In the above syntax for slicing, the list_name refers to the name of the list on which we want to perform slicing, the starting_index refers to the index of the list from which we have to slice our list and the ending_index refers to the index till which we have to slice our list. The step is the number of elements we want to skip before selecting the next element.

To select only the even indexed elements, we can set our starting_index as 0, ending_index as the list length, and step as 2. To select only the odd indexed elements, we can set our starting_index as 0, ending_index as the list length, and step as 2. Setting the step as 3, it will print every 3rd element in the list.

By default, the starting_index is set to 0, the ending index is set to the length of the list, and the step to 1. We have the option to keep the positions of the starting_index, ending_index, and the step blank. The interpreter will take the default values if we don’t pass a parameter(Leave the positions of starting_index, ending_index, and step as blank). So, when we don’t pass the starting_index the interpreter assumes the starting_index as 0 and prints the list from the beginning. Similarly, when we don’t pass the ending_index, it prints the list till the very end. And in the case of step count by default, it prints every next element(step=1).

I hope that helped you to understand what is slicing thoroughly. You can refer to the link below to learn more about slicing.

Related: Learn more about slicing in Python.

Now, we can move to our main problem.

What is [:] exactly?

Now that we have understood slicing we will try to understand what [:] means. [:] is simply a slicing query without any parameters passed.

Example: my_list[:]

This means the starting_index is 0 and the ending _index is length of the list ( In the above example [:] is equivalent to [0:5] ). So, the entire list is sliced. When the interpreter encounters the slicing query it will start printing the elements from the beginning of the list till the end of the list and then stop, as mentioned in the default parameters.

In simple words, we can say it’s just another way of printing the list. The output print(my_list[:]) is similar to print(my_list). There’s no difference at all between them. We can read the query as print the slice of the list starting from the 0th index to len(my_list)th index with a step size of 1.

This can be done on lists, arrays, strings, or even on tuples. The slicing method works similarly on all these data structures. So, if we slice a string with [:] it will simply print the entire string.

Using [:] on a string

string = "This is a string"
print(string[:])
Code And Output For String Slicing In Python 1
Code And Output For String Slicing In Python

In the above code, we performed slicing on a string. String slicing works in the same way as list slicing. When we print string[:], similar to the list it will print the entire string from start to end as the default parameters are starting_index = 0, ending_index = string length, and the step size is 1. Therefore, the above code produces the output, “This is a string”.

Strings are very important in programming and are very hard to work upon as they are immutable. String slicing gives strings capabilities that make strings very easy to work on.

Conclusion

Basically, the [:] is a slicing method that is performed on lists or strings to print the entire list or string respectively instead of a slice. It’s just another way to print the list or string with a few extra and unnecessary characters. We don’t have a use for it as we can simply print the list itself if we want the entire list as output. But, something like this exists is what we learned today.

References

Stack Overflow answer on the same question.

Official Python documentation on slicing.