In this article, we will learn about slicing in Python Programming Language. Slicing is used to cut a part of string. Characters can be cut from the beginning or from end, also from a given index number.
Table Of Contents
What is an Index Number?
Index is used to access the String elements. It starts from 0, it means index of first character in string is 0, and index of nth character in string is n-1. Also, we have negative index values, which starts from -1. The last character is often considered at the index value -1. Below is an example of string.
CODE :
# initialized a string example_str = 'Python' # type() will print the data type of var example_str # which happens to be of type string. print(type(example_str))
OUTPUT :
Frequently Asked:
<class 'str'>
Below is a reference for index number.
IMAGE
So now lets dive deep in Slicing in Python and learn about some methods through which we can do slicing in Python.
Slicing in Python using Slice() function
The slice() function in Python, creates a slice object, which can be used for slicing in Python. Data types like string, list, tuple can be sliced using the slice() function.
Latest Python - Video Tutorial
SYNTAX:
slice(start,stop,end)
It accepts three parameters:
- start: from which index number slicing should start.
- stop: index number where slicing should stop.
- step: optional, this skips the number in iteration, if step is 2 then it will skip each 2nd char in slicing.
CODE :
# initialized a string example_str = 'This is Python Programming language.' # initialzied a list example_lst = ['Python','Java','kotlin','C'] # initialized a tuple. example_tuple = ('React','Spring','Django','Node') # type() will print the data type of var example_str which happens to be of class str. print(type(example_str)) # type() will print the data type of var example_lst which happens to be of class list. print(type(example_lst)) # type() will print the data type of var example_tuple which happens to be of class tuple print(type(example_tuple)) # creating a slice object which will be later used for slicing. slice = slice(1,8) # slicing example_str print(example_str[slice]) # slicing in example_tuple print(example_tuple[slice]) #slicing in example_lst print(example_lst[slice])
OUTPUT :
<class 'str'> <class 'list'> <class 'tuple'> his is ('Spring', 'Django', 'Node') ['Java', 'kotlin', 'C']
So in the code and output above you can see how slice() function has been used for Slicing in Python, also you can see slice() function can be used with other class types like tuple and list.
Slicing in Python using Index number
We can also do Slicing in Python just by cutting string with the help of index numbers. As you are familiar with Index number, so just input the index number in big braces separated by colon. SYNTAX: string[a:b]
, here a and b are variables where you can put the index number you want. It will return a string containing characters from index a till b-1. See an example below.
CODE :
# initialized a string example_str = 'abcdefgh' # type() will print the data type of var example_str which happens to be of class str. print(type(example_str)) print('String: ',example_str ) # slicing string using Index number print('Sliced String: ',example_str[2:6])
OUTPUT :
<class 'str'> String: abcdefgh Sliced String: cdef
In the example above, we have a string and we used the slicing with index numbers to slice some chars of example string. This is another easy way to do slicing in Python. By this method you can also slice elements of list and tuples too.
Negative Indexing
Negative indexing is often referred as slicing from the end of the string, tuple, list or any other class type. We use neagtive index numbers that start from -1 to achieve Slicing in Python. See an example below.
CODE :
# initialized a string example_str = 'abcdefgh' # type() will print the data type of var example_str which happens to be of class str. print(type(example_str)) print('String: ',example_str ) # Negative Slicing print('Sliced String: ',example_str[-5:-2])
OUTPUT :
<class 'str'> String: abcdefgh Sliced String: def
In this method, you can see Negative Indexing can also be used for Slicing in Python. Negative Indexing can also be used along with slice object.
Slicing from Start and End
Another way of Slicing in Python is to slice or eliminate all the elements/char of the string from the first or last character. Let’s look them one by one.
- Slicing from Start
By using index number we can eliminate elements/chars from the starting of a string, list, tuple and other mutable. See the example below.
CODE :
# initialized a string example_str = 'abcdefgh' # type() will print the data type of var example_str which happens to be of class str. print(type(example_str)) print('String: ',example_str ) # Slicing from start print('Sliced String: ',example_str[2:])
OUTPUT :
<class 'str'> String: abcdefgh Sliced String: cdefgh
Summary
In this Python tutorial we learned about Slicing in Python. Also we learned about Index numbers, Negative Indexing and slice() object. Using Index numbers with a start,stop and step parameters can be most convenient and easy for Slicing in Python. But if there are different class types of data and you need same slicing in all of the class types, then you can define a slice parameter using the slice object.
Make sure to practice with the above examples, to have a better understanding of this problem. Thanks.
Latest Video Tutorials