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 |
|---|---|
|
First 3 characters |
|
From index 2 to end |
|
Last character |
|
Reverse string |