🐍1.7Python String Slicing Explanation
Python String Slicing Explained (Using 'Divya')
Introduction to String Slicing
String slicing in Python allows you to extract a specific portion of a string using the syntax:
s[start:end:step]
start
: The index where slicing begins.end
: The index where slicing stops (not included in the output).step
: Defines the step size (positive for forward, negative for reverse).
For demonstration, let's take the string:
s = "Divya"
Indexing Table
Index | D | i | v | y | a |
---|---|---|---|---|---|
Forward Index | 0 | 1 | 2 | 3 | 4 |
Backward Index | -5 | -4 | -3 | -2 | -1 |
1️⃣ Forward Slicing Cases
Case 1: s[1:-1:]
print(s[1:-1:]) # "ivy"
Breakdown:
start = 1
→ Starts from 'i'.end = -1
→ Stops before 'a'.step = 1
(default) → Moves forward one character at a time.
✅ Result: "ivy"
Case 2: s[0:3]
print(s[0:3]) # "Div"
Breakdown:
start = 0
→ Starts from 'D'.end = 3
→ Stops before 'y'.
✅ Result: "Div"
Case 3: s[:3]
(Implicit Start)
print(s[:3]) # "Div"
Same as s[0:3]
because start
is omitted (defaults to 0
).
✅ Result: "Div"
Case 4: s[2:]
(Implicit End)
print(s[2:]) # "vya"
start = 2
→ Starts from 'v'.end
omitted → Runs until the end.
✅ Result: "vya"
Case 5: s[::2]
(Skipping Characters)
print(s[::2]) # "Dva"
step = 2
→ Takes every 2nd character.
✅ Result: "Dva"
2️⃣ Reverse Slicing Cases
Case 6: s[::-1]
(Reverse String)
print(s[::-1]) # "ayviD"
step = -1
→ Reads the string backward.
✅ Result: "ayviD"
Case 7: s[1::-1]
print(s[1::-1]) # "iD"
Breakdown:
start = 1
→ Starts at 'i'.step = -1
→ Moves backward.
✅ Result: "iD"
Case 8: s[3:0:-1]
print(s[3:0:-1]) # "yvi"
Breakdown:
start = 3
→ Starts at 'y'.end = 0
→ Stops before 'D'.step = -1
→ Moves backward.
✅ Result: "yvi"
Case 9: s[-1:-4:-1]
print(s[-1:-4:-1]) # "ayv"
start = -1
→ Starts at 'a'.end = -4
→ Stops before 'D'.step = -1
→ Moves backward.
✅ Result: "ayv"
3️⃣ Edge Cases
Case 10: Empty Slice (s[3:3]
)
print(s[3:3]) # ""
Since start
and end
are the same, no characters are extracted.
✅ Result: ""
Case 11: Out-of-Bounds (s[10:15]
)
print(s[10:15]) # ""
If indices are out of range, Python doesn't raise an error, it returns an empty string. ✅ Result: ""
Summary Table
Expression | Result | Explanation |
---|---|---|
s[1:-1:] |
"ivy" | Forward slice from 'i' to 'y', excluding 'a'. |
s[1::-1] |
"iD" | Reverse slice from 'i' to 'D'. |
s[::-1] |
"ayviD" | Full reverse string. |
s[3:0:-1] |
"yvi" | Backward slice from 'y' to 'i'. |
s[-1:-4:-1] |
"ayv" | Backward slice using negative indices. |
s[:3] |
"Div" | First three characters. |
s[2:] |
"vya" | Everything from 'v' onward. |
s[::2] |
"Dva" | Every second character. |
s[3:3] |
"" | Empty slice. |
s[10:15] |
"" | Out-of-bounds slice. |
Conclusion
String slicing is a powerful tool in Python that helps in extracting substrings efficiently. By understanding different slicing patterns, you can manipulate strings easily and write cleaner code.
I hope this guide clarifies all possible slicing cases! 🚀 Let me know if you have any questions. 😊
Comments
Post a Comment