Python: Slice Notation on String
Introduction
The term slicing in programming usually refers to obtaining a substring, sub-tuple, or sublist from a string, tuple, or list respectively.
Python offers an array of straightforward ways to slice not only these three but any iterable. An iterable is, as the name suggests, any object that can be iterated over.
In this article, we’ll go over everything you need to know about Slicing Strings in Python.
Slicing a String in Python
There are a couple of ways to slice a string, most common of which is by using the :
operator with the following syntax:
string[start:end]
string[start:end:step]
The start
parameter represents the starting index, end
is the ending index, and step
is the number of items that are “stepped” over.
Let’s go ahead and slice a string:
string = 'No. I am your father.'
print(string[4:20])
This will omit the first four