Use text[start:stop:step] to select part of a Python string. The start position is included and the stop position is excluded, so word[0:2] returns "Py" from "Python". Omitting bounds, using negative indices, or adding a step lets you select from the beginning, count from the end, or move backward.
Python string-slicing syntax
A slice has the form text[start:stop:step]. The colon separates its optional start, stop, and step values. With the usual positive step, Python moves from left to right, including the start position but stopping before the stop position. The stop is a boundary, not another selected character.
word = "Python"
word[0:2] # "Py"
word[2:5] # "tho"
Think of indices as positions between characters: word[0:2] selects the characters between boundaries 0 and 2. This rule also means adjacent slices join back together: text[:i] + text[i:] == text.
Omit bounds to slice from an edge
For a positive step, an omitted start means the beginning of the string, and an omitted stop means the end.
Free tools Windows power users keep installed
One-click scans. No signup required.
#1 Best Overall
word[:2] # "Py"
word[4:] # "on"
word[:] # "Python"
Use negative indices to count from the end
Negative indices count backward from the string’s end: -1 is the final position, -2 the one before it, and so on.
word[-2:] # "on"
word[:-1] # "Pytho"
-0 is the same value as 0, so use -1, not -0, to refer to the last character.
Rank #2
Use a step to skip positions or reverse a string
The optional step controls how many positions to advance between selected characters. A positive step moves forward; a negative step moves backward. A step of zero is invalid.
word[::2] # "Pto"
word[::-1] # "nohtyP"
word[::-1] is the common full-string reversal idiom: the negative step traverses the string backward. With a negative step, omitted bounds follow the reverse traversal’s boundary behavior, so do not assume they behave just like omitted bounds with a positive step.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →Out-of-range slices and indexing are different
Python adjusts slice bounds that go beyond the string’s length. For example, word[0:100] returns the whole string rather than raising an error. A positive-step slice whose start is at or beyond its stop is empty.
word[0:100] # "Python"
word[4:2] # ""
This forgiving behavior applies to slices, not ordinary single-position indexing: asking for a nonexistent position, as in word[100], raises IndexError.
Slicing makes a selection; it does not edit the string
Python strings are immutable, so slicing returns a string and cannot change the original in place. To replace a portion, concatenate the unchanged parts with the replacement to make a new string:
text = "Python"
start, stop = 0, 2
replacement = "Py"
changed = text[:start] + replacement + text[stop:]
For a different replacement, change replacement; the original text remains unchanged.
Best Value
What a string slice counts
Python strings are sequences of Unicode code points, and a single index returns a string of length one—there is no separate character type. Slices therefore select code points, not necessarily whole user-perceived characters. A displayed character composed of a base code point and combining marks, or an emoji formed from multiple code points, may be split by a slice.
Quick Recap
Further reading in the Python documentation
- Built-in Types — Python 3.14.8 documentation explains sequence slicing and the
strtype. - An Informal Introduction to Python — Python 3.14.8 documentation walks through slicing examples, omitted bounds, negative indices, and the exclusive stop position.
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




