String Slicing in Python
In this tutorial, you will learn what is string slicing in Python. How many ways you can slice a string and what is it used for. For better understanding, we have lots of examples.
What is String Slicing in Python?
String slicing is a process of extacting a part of a string also called as substring.
It is a very useful feature of Python. While working with strings you will frequently use this feature.
For example, you can extract the first character of a string, last character of a string, first 5 characters of a string, last 5 characters of a string, etc.
"Python" => "p" (first character) "Python" => "n" (last character) "Python" => "Pytho" (first 5 characters) "Python" => "ython" (last 5 characters)
There are many ways to slice a string in Python. We will discuss all of them in this tutorial.
String Slicing Methods
There are two ways to slice a string in Python.
- Using slice() constructor
- Using [:] operator
Let's see the syntax of both the methods.
1. Using slice() constructor
The slice() is a constructor in python that returns a slice object. This retured object than later can be used with any string to slice it.
Here is the syntax of slice() constructor.
slice(start, stop)
# or
slice(start, stop, step)
Here:-
- start is the starting index of the slice. It is optional. If not provided, it is considered as 0.
- stop is the ending index of the slice. It is required. The character at this index is not included in the slice.
- step is the step size of the slice. It is optional. If not provided, it is considered as 1.
Let's see some examples.
# using slice() constructor
str1 = "Python is awesome"
str2 = "TutorialsTonight is best to learn Python"
# create object
# slices the string from index 0 to 8
slice1 = slice(0, 9)
# slice the string using slice object
print(str1[slice1]) # Python is
print(str2[slice1]) # Tutorials
Python is Tutorials
Note: Once you create a slice object, you can use it with any string any number of times to slice it.
Number of arguments:
- If only 1 argument is passed to slice() constructor, it is considered as stop and start is considered as 0.
- If only 2 arguments are passed then, it is considered as start and stop.
- If only 3 arguments are passed then, it is considered as start, stop and step.
# using slice() constructor
str = "Python is awesome"
# only one argument
slice1 = slice(4)
print(str[slice1]) # Pyth
# two arguments
slice2 = slice(4, 9)
print(str[slice2]) # on is
# three arguments
slice3 = slice(0, 16, 2)
print(str[slice3]) # Pto saeo
Pyth on is Pto saeo
Using negative indices:
You can also use negative indices to slice a string.
Let's see an example.
# using slice() constructor
str = "Python is awesome"
# using negative indices
slice1 = slice(-7, -1)
print(str[slice1]) # awesom
slice2 = slice(-6, -2)
print(str[slice2]) # weso
awesom weso
2. Using [:] operator
The [:] operator can also be used to slice a string in Python. Infact it is the most commonly used method to slice a string.
Mainly it is used to slice list so known as list slicing. But it can also be used to slice a string.
Here is the syntax of [:] operator.
str[start]
# or
str[start:stop]
# or
str[start:stop:step]
Here:
- start (optional) is the starting index of the slice. Default value is 0.
- stop (optional) is the ending index of the slice. Default value is the length of the string. The character at this index is not included in the slice.
- step (optional) is the step size of the slice. Default value is 1.
Let's see some examples.
# using [:] operator
str = "Python is awesome"
# slice the string from index 0 to 8
print(str[0:9]) # Python is
Python is
variations of [:] operator
str[start:stop] => slice from 'start' to 'stop-1' str[start:] => slice from 'start' to the end of the string str[:stop] => slice from the start of the string to 'stop-1' str[:] => slice the whole string str[start:stop:step] => slice from 'start' to 'stop-1' with the step 'step'
Look at the example below.
# using [:] operator
str = "Python is awesome"
# only start given
print(str[8:]) # s awesome
# only stop given
print(str[:8]) # Python i
# start and stop given
print(str[4:9]) # on is
# none given
print(str[:]) # Python is awesome
# start, stop and step given
print(str[0:16:2]) # Pto saeo
s awesome Python i on is Python is awesome Pto saeo
Using negative indices:
YOu know it that you can access the elements of a string using its index value, but this is also true for negative index. For example, the last character of a string can be accessed using -1 index str[-1]
.
# using [:] operator
str = "Python is awesome"
# using negative indexing
print(str[-8:]) # awesome
print(str[-8:-1]) # awesom
print(str[-8:-1:2]) # wsm
awesome awesom wsm
Reverse string using slicing
To reverse a string in Python, you can use slicing.
For this you need to use the [:] operator and pass the step value as -1.
# reverse a string
str = "Python"
reverse = str[::-1]
print(reverse) # nohtyP
nohtyP
Copy string using slicing
To copy a string in Python, simply take string slicing from 0 to the length of the string and assign it to a new variable.
# copy a string
str = "Python"
copy = str[:]
print(copy) # Python
Python
Conclusion
Slicing a string in Python is a very useful technique. You can use it to get substring from string, reverse a string, copy a string, check if a string is palindrome, etc.
We have seen 2 different ways to slice a string among which the [:] operator is the most commonly used.
So, this was all about slicing a string in Python. I hope you liked it.
Happy coding!😊