Computer >> Computer tutorials >  >> Programming >> Python

How to print characters from a string starting from 3rd to 5th in Python?


Slicing feature in Python helps fetch a substring from original string. Slice operator [:] needs two operands. First operand is an integer representing index of starting character of slice. Second operand is index of character next to slice. Recalling that index of sequence starts from 0,

>>> string = 'abcdefghij'
>>> string[2:5]
 'cde'

Here 3rd character of slice ‘cde’ starts at index 2 and ends at 4, so second operand is given as 5