MWZ

MINDWAREZONE

String Slicing

String slicing is a technique used to extract a specific portion or substring from a string.  
Slicing extracts a portion of a string.

Syntax

string[start:end:step]
            

Example 1:

text = "Python"
print(text[0:3])
            

Output

Pyt
            

Example 2:

text = "Python"
print(text[2:])
            

Output

thon
            

Example 3

text = "Python"
print(text[::-1])
            

Output

nohtyP
            

Common Slicing Patterns

Slice Result
text[:3] First 3 characters
text[2:] From index 2 to end
text[-1] Last character
text[::-1] Reverse string