Slicing in Python is a powerful technique used to extract a portion (substring) of a string (or any sequence like list, tuple, etc.) without modifying the original data.
It allows you to access parts of a string using index positions and supports positive and negative indexing.
🔹 Basic Syntax of Slicing
string[start : stop : step]| Parameter | Description |
|---|---|
| start | Index to begin the slice (inclusive). Default → 0. |
| stop | Index to stop the slice (exclusive). Default → end of string. |
| step | Number of steps to move (default → 1). Used for skipping characters or reversing. |
🧩 Example String
text = "PythonProgramming"| Character | P | y | t | h | o | n | P | r | o | g | r | a | m | m | i | n | g |
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 |
| Negative Index | -17 | -16 | -15 | -14 | -13 | -12 | -11 | -10 | -9 | -8 | -7 | -6 | -5 | -4 | -3 | -2 | -1 |
🔹 1. Basic Slicing Examples
print(text[0:6]) # From index 0 to 5
print(text[6:]) # From index 6 to end
print(text[:6]) # From start to index 5
print(text[:]) # Entire stringOutput:
Python
Programming
Python
PythonProgramming✅ Notes:
- The start index is inclusive.
- The stop index is exclusive.
- If you omit start or stop, Python assumes the beginning or end of the string.
🔹 2. Using Negative Indexing
Negative indices count from the end of the string (−1 = last character).
print(text[-6:]) # Last 6 characters
print(text[:-6]) # All except last 6
print(text[-11:-6]) # Slice from -11 to -7Output:
amming
PythonPro
ogram✅ Use negative slicing to extract parts of a string from the end.
🔹 3. Using the Step Value
The step defines how much the index increases after each iteration.
print(text[::2]) # Every 2nd character
print(text[1::3]) # Every 3rd character starting from index 1Output:
Pto rgamn
yhrga✅ Step value can help you skip characters or sample patterns within the string.
🔹 4. Reversing a String Using Slice
The most popular slicing trick:
print(text[::-1]) # Reverse stringOutput:
gnimmargorPnohtyP✅ Setting step = -1 reverses the direction of traversal.
🔹 5. Combining Start, Stop, and Step
print(text[0:12:2]) # From index 0 to 11, every 2nd char
print(text[2:15:3]) # From index 2 to 14, every 3rd char
print(text[-10:-2:2]) # From -10 to -3, every 2nd charOutput:
Pto rg
tPgm
ormn🔹 6. Slicing Beyond String Length
If you specify indices beyond the string’s range, Python doesn’t throw an error — it just stops at the string’s boundary.
print(text[0:100]) # Stop > string length
print(text[-100:]) # Start < 0Output:
PythonProgramming
PythonProgramming✅ Slicing in Python is safe — it doesn’t raise IndexError.
🔹 7. Omitting Parameters
| Slice | Equivalent To | Description |
|---|---|---|
text[:] | text[0:len(text)] | Returns full string |
text[:n] | text[0:n] | From start to index n−1 |
text[n:] | text[n:len(text)] | From index n to end |
text[::-1] | Reverse string | Traverses backward |
🔹 8. Real-Life String Manipulation Examples
🧩 Extracting Substring
email = "dheeraj@example.com"
username = email[:email.index("@")]
domain = email[email.index("@")+1:]
print(username, domain)Output:
dheeraj example.com🧩 Get First and Last Character
word = "Python"
print(word[0], word[-1])Output:
P n🧩 Remove First and Last Character
print(word[1:-1])Output:
ytho🧩 Reverse Words in a Sentence
sentence = "Python is awesome"
reversed_sentence = sentence[::-1]
print(reversed_sentence)Output:
emosewa si nohtyP🔹 9. Slicing in Other Sequences
Slicing works the same way for other sequence types like lists, tuples, and byte arrays.
nums = [10, 20, 30, 40, 50, 60]
print(nums[1:4]) # [20, 30, 40]
print(nums[::-1]) # [60, 50, 40, 30, 20, 10]🧾 Summary Table
| Operation | Example | Result |
|---|---|---|
| Slice from start | text[:6] | 'Python' |
| Slice to end | text[6:] | 'Programming' |
| Middle slice | text[0:6] | 'Python' |
| Negative slice | text[-6:] | 'amming' |
| Step slice | text[::2] | 'Pto rgamn' |
| Reverse | text[::-1] | 'gnimmargorPnohtyP' |
| Safe slicing | text[0:100] | Returns full string |
| Skip characters | text[::3] | 'PhPgai' |
✅ In short:
Slicing in Python lets you extract substrings efficiently using
[start:stop:step].
- Works with positive & negative indices.
- Default slice
[::]gives the full string.- Safe, flexible, and non-destructive.
- Common trick:
[::-1]→ reverse any string instantly.
