Strings
String Slicing
Accessing substrings with slicing
Interview: Common interview topic
Last Updated: June 12, 2026
•
6 min read
Slicing is Python's powerful syntax for extracting substrings using the [start:stop:step] notation. It's one of the most frequently tested string topics in interviews.
Slicing Syntax
s[i]— single character at index i (0-based, negative counts from end)s[start:stop]— substring from start to stop-1s[start:stop:step]— every step-th characters[:n]— first n characterss[n:]— from index n to ends[::-1]— reverse the string
Key Rules
- Slicing never raises
IndexError— out-of-range indices are handled gracefully stopis exclusive:s[0:3]gives indices 0, 1, 2- Negative indices count from end:
s[-1]is the last character - Negative step reverses direction:
s[::-1]reverses the entire string - Slicing creates a new string object (shallow copy for strings)
Use Cases
Reversing strings for palindrome checking
Extracting substrings for text parsing
Skipping characters with step slicing
Safe substring extraction without index bounds checking
Common Mistakes
Forgetting that stop index is exclusive (off-by-one errors)
Confusing negative indices with step direction
Thinking slicing modifies the original string (it creates a new one)
Not knowing that out-of-range slices return empty strings instead of raising errors