String slicing

Strings support slicing, which means getting characters by a specified range from your string. Let's take a look at the following example. Note that starting index value is always included and an end value is always excluded.

Consider a string, str = "Programming":

>>> str[0:2]
'Pr'
>>> str[2:5]
'ogr'

Now, the default of an omitted first index is zero, as in the example:

>>> str[:2] + str[2:]
'Python'
>>> str[:4] + str[4:]
'Python'
>>> str[:2]
'Py'
>>> str[4:]
'on'
>>> str[-2:]
'on'
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset