Python provides a new operator, namely the slice operator. With the help of a slice operator, we can divide the objects as per our requirement. Also, we can use this operator on all types of data types in python, like list, tuple, string, etc. Python also provides a slice() function. You can specify the dividing range to the slice operator and slice function with the help of parameters.
Table of Contents
slicing in python using indexing
Object slicing with indexing is the fastest way to break down the object in a particular format. Let’s understand how to use the slice operator in different ways. We will take the “hello world” string for all the examples below.
1. Passing a positive integer after slice operator to string slicing in python.
Example
str = "Hello world"
print(str[:3])
Output:
Hel
2. Passing a negative integer after slice operator.
Example
str = "Hello world"
print(str[:-3])
Output:
Hello wo
3. Passing a positive integer before slice operator.
Example
str = "Hello world"
print(str[6:])
Output:
world
4. Passing a negative integer before slice operator.
Example
str = "Hello world"
print(str[-3:])
Output:
rld
5. Passing two positive parameters first is the starting index, and the second is the ending index.
Example
str = "Hello world"
print(str[2:9])
Output:
llo wor
6. Passing two negative parameters first is the starting index, and the second is the ending index.
Example
str = "Hello world"
print(str[-10:-3])
Output:
ello wo
7. Passing positive integer no slice operator in an index.
Example
str = "Hello world"
print(str[8])
Output:
r
8. Passing negative integer no slice operator in an index.
Example
str = "Hello world"
print(str[-8])
Output:
1
9. A slice operator is also used to reverse a string.
Slicing in Python is also used for reversing the string. Slicing is the most efficient way to reverse the string compared to other reversing techniques in Python. Programmers usually use a slice operator as it takes less time to reverse the string. Let’s understand this with an example.
Example
str = "Hello world"
print(str[::-1])
Output:
dlrow olleH
10. Passing the third positive parameter to the slice operator for skipping items.
Example
str = "Hello world"
print(str[0:9:2])
Output:
Hlowr
11. Passing the third negative parameter to the slice operator for skipping items.
Example
str = "Hello world"
print(str[-9:-1:2])
Output:
lowr
Slicing list in python
Slicing a list in python is also possible. The slice operator will slice the data items stored in the list. We can use the slice operator on the list in the same way as we use on a string.
Example
list = ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
print("Printing first four values",list[:4])
print("Printing last three values",list[7:])
print("Printing first eight values",list[:-2])
print("Printing last eight values",list[-8:])
print("Printing third item to eightth item of the list",list[3:8])
print("Printing secong item to ninth item of the list",list[-9:-1])
print("Passing the third parameter to step up the indices", list[0:11:2])
Output:
Printing first four values ['one', 'two', 'three', 'four']
Printing last three values ['eight', 'nine', 'ten']
Printing first eight values ['one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight']
Printing last eight values ['three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten']
Printing third item to eightth item of the list ['four', 'five', 'six', 'seven', 'eight']
Printing secong item to ninth item of the list ['two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine']
Passing the third parameter to step up the indices ['one', 'three', 'five', 'seven', 'nine']
Slicing in python using slice() function
The slice () function returns the particular part of any object if used with any string for making the slice operation where an object or array breaks down in several formats. Usually, the slice function is not useful for the programmers. They prefer to use indexing for slicing any object.
Parameter values to the slice() function
Syntax – slice(start, end, step)
- Start(optional) – This parameter is for starting index of the object.
- End – This parameter is for the ending index of the object.
- Step(optional) – Step(optional) – This parameter skips an index from an object’s start to an object’s end.
Example of slicing in python using a slice() function.
- Slicing string in python using slice() function.
Same as slicing a string using indexing, we can also slice it using the slice function. The slice function will give output as the slice operator. But the execution time of the slice() function is more compared to the slice operator. Let’s learn how to use the slice function.
Example
str = "hello world"
s1 = slice(3)
s2 = slice(-3)
s3 = slice(2,6)
s4 = slice(-9,-1)
s5 = slice(0,9,2)
print(str[s1])
print(str[s2])
print(str[s3])
print(str[s4])
print(str[s5])
Output:
hel
hello wo
llo
llo worl
hlowr
2. Using slice() function slicing list in python.
We can also use the slice() function on the list. The slice function works on the data items stored in the list. We can print the list’s data item the way we want using slice() functions parameters.
Example
list = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j']
l1 = slice(4)
l2 = slice(-8)
l3 = slice(4,9)
l4 = slice(-7,-2)
l5 = slice(3,8,2)
print(list[l1])
print(list[l2])
print(list[l3])
print(list[l4])
print(list[l5])
Output:
['a', 'b', 'c', 'd']
['a', 'b']
['e', 'f', 'g', 'h', 'i']
['d', 'e', 'f', 'g', 'h']
['d', 'f', 'h']
3. Slicing a tuple using the slice() function
Slicing a tuple is also possible in python. The slice() function will work the same as the list and tuple. Passing parameters to the slice function will return the particular part of the tuple.
Example
my_tuple = (1, "hello", "python", "a", 100, "42", 90, 45, "java", 1023)
t1 = slice(1)
t2 = slice(-7)
t3 = slice(2,7)
t4 = slice(-8,3)
t5 = slice(4,9,2)
print(my_tuple[t1])
print(my_tuple[t2])
print(my_tuple[t3])
print(my_tuple[t4])
print(my_tuple[t5])
Output:
(1,)
(1, 'hello', 'python')
('python', 'a', 100, '42', 90)
('python',)
(100, 90, 'java')