Slicing is how you pull out a portion of a string (or any sequence, like a list) by position, without writing a loop. It's one of Python's most useful shortcuts once it clicks, and it uses a compact [start:stop:step] syntax that shows up constantly in real code.
Basic Slice Syntax
text = "Simple2Code"
print(text[0:6]) # characters at index 0 up to (not including) 6
print(text[6:]) # from index 6 to the end
print(text[:6]) # from the start up to index 6
print(text[:]) # the whole string (a copy)
Output:
Simple
2Code
Simple
Simple2Code
Just like range(), the stop index is always exclusive — text[0:6] gives you characters at positions 0 through 5, not 6.
Negative Indices
Negative numbers count backward from the end of the string, which is handy for grabbing the last few characters without knowing the string's length:
text = "Simple2Code"
print(text[-4:]) # last 4 characters
print(text[:-4]) # everything except the last 4 characters
print(text[-1]) # the very last character
Output:
Code
Simple2
e
The step Argument and Reversing a String
The third slice value controls the step size — skipping characters, or even walking backward through the string:
text = "Simple2Code"
print(text[::2]) # every second character
print(text[::-1]) # the entire string, reversed
Output:
Smlecd
edoC2elpmiS
text[::-1] is the famous one-liner for reversing a string in Python — a step of -1 means "walk through the whole sequence back to front." You can see this used in a complete worked example in the Python string reversal program, alongside a manual loop-based alternative for comparison.
Slicing Never Raises an IndexError
Unlike indexing a single character with text[50], which raises an IndexError if the index is out of range, slicing is forgiving — text[0:1000] on a short string simply returns whatever characters exist, with no error. This makes slicing safer than manual index arithmetic when you're not sure exactly how long a string will be.
