Slicing & Indexing0%
Slicing & Indexing
Beginner9 min read•Updated: 2026-09-03
Slicing & Indexing
Python's indexing and slicing provide unmatched convenience for extracting substrings.
Key Concepts & Detailed Explanation
Indexing:
- Forward indexing begins at 0.
- Negative indexing begins at -1 (the last character).
Slicing syntax: [start:stop:step]
- start: inclusive start index (defaults to 0).
- stop: exclusive stop index (defaults to end of string).
- step: stride (defaults to 1; -1 traverses backwards).
Code Examples & Output
Python
tech = "MSK Institute"
print("First char [0]:", tech[0]) # 'M'
print("Last char [-1]:", tech[-1]) # 'e'
print("Slice [0:3]:", tech[0:3]) # 'MSK'
print("Step slice [::2]:", tech[::2]) # Every second char
print("Reversed [::-1]:", tech[::-1]) # 'etutitsnI KSM'
Expected Output:
Output
First char [0]: M
Last char [-1]: e
Slice [0:3]: MSK
Step slice [::2]: MKIttt
Reversed [::-1]: etutitsnI KSM
Best Practices & Common Pitfalls
Remember that 'stop' is exclusive: text[1:4] extracts characters at indices 1, 2, and 3.
Practice Quiz
1. What does 'Python'[1:4] return?
- A) 'Pyt'
- B) 'yth'
- C) 'ytho'
- D) 'thon'
Answer: B
Explanation: Indices 1, 2, 3 give 'yth'.
2. What is the most concise way to reverse a string 's' in Python?
- A) s.reverse()
- B) s[::-1]
- C) reverse(s)
- D) s[-1:0]
Answer: B
Explanation: s[::-1] reverses the string using slice step -1.
Practice Challenge
Extract the domain name from an email address 'student@mskinstitute.in' using slicing.
Next Lesson
Modifying Strings
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| String Introduction | Modifying Strings |
Practice Quiz
Test your understanding of this lesson with 1 questions. Each question has one correct answer.