☞String slices refers to part of a string containing some contiguous characters from the string.
☞Slicing a String Using Slice Operator [ : : ] :
Syntax :<string> [begin_index : end_index : step]
0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 |
I | N | S | P | I | R | E | W | E | B | S | O | F | T | ||
-16 | -15 | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
S="INSPIRE WEB SOFT" print(S[6:13]) print(S[6:20]) print(S[2:-2]) print(S[-6:14]) print(S[-6:20]) print(S[-6:8:-1]) print(S[2:14:2])
E WEB S E WEB SOFT SPIRE WEB SO B SO B SOFT BE SIEWBS
☞Reversing a String Using Slice Operator [ : : ] :
Example :
S="INSPIRE WEB SOFT"
print(S[15:0:-1]) #I will not include
print(S[15::-1])
print(S[len(S)::-1])
print(S[::-1])
print(S[20::-1])
TFOS BEW ERIPSN TFOS BEW ERIPSNI TFOS BEW ERIPSNI TFOS BEW ERIPSNI TFOS BEW ERIPSNI