Strings Slices in Python

Strings are sliced using a range of indices. string1[a : b] where a and b are integers and legal indices ,will return a slice of the string by returning the characters falling between indices a and b : starting at a, a+1, a+2 ….. till b-1.

Example : string1=”abcdefghijk”
then, string1[2:10]
 will return     "defghij"  
string1[3:] return indices 4 to last and string1[:10] returns indeces 0 to 9.
To slice last 3 use string[:-3] and to slice first 5 use string[4:]

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top