TypeError: slice indices must be integers or None (tested fix)
TypeError: slice indices must be integers or None or have an __index__ method means a slice bound such as x[:mid] is a float or a string. We reproduced it in Python 3.13, compared it with the plain-indexing error, and tested the fixes.
1. A float or string in a slice
Slice bounds must be int, None, or an object with __index__. A float (usually from /) or a string (from input() or a URL) fails. Plain indexing with the same bad value gives a different message:
s, items = "abcdef", [10, 20, 30, 40]
mid = len(items) / 2 # 2.0, a float
for expr in ["items[:mid]", "s[1.0:3]", "s['1':]", "items[mid]", "items[None]"]:
try:
print(f"{expr:12} OK -> {eval(expr)!r}")
except TypeError as e:
print(f"{expr:12} TypeError: {e}")
Output:
items[:mid] TypeError: slice indices must be integers or None or have an __index__ method
s[1.0:3] TypeError: slice indices must be integers or None or have an __index__ method
s['1':] TypeError: slice indices must be integers or None or have an __index__ method
items[mid] TypeError: list indices must be integers or slices, not float
items[None] TypeError: list indices must be integers or slices, not NoneType
So slice indices must be integers or None comes from x[a:b], and list indices must be integers or slices, not float comes from x[i]. Both have the same cause.
2. The fix: //, int(), and None for open ends
s, items = "abcdef", [10, 20, 30, 40]
mid = len(items) // 2
print(items[:mid], items[mid:])
start = "1" # e.g. from input() or a query string
print(s[int(start):])
end = None # None is allowed in a slice: means "to the end"
print(s[2:end], s[slice(1, None, 2)])
Output:
[10, 20] [30, 40]
bcdef
cdef bdf
Use // to compute midpoints and offsets, and convert strings with int(). None is valid in a slice and means "from the start" or "to the end", so you don't need a sentinel like len(s). A class with an __index__ method is accepted too. We checked one returning 2: "abcdef"[I():] gave 'cdef'. For other places that need an int, see 'float' object cannot be interpreted as an integer.
Frequently asked questions
How do I fix slice indices must be integers or None?
Make the bounds ints: use // instead of / (len(x) // 2), or int() for strings from input or URLs.
What is the difference from list indices must be integers or slices, not float?
Same cause. The slice message comes from x[a:b]; the list-indices message comes from x[i].
Can I use None in a slice?
Yes. None means from the start or to the end, so s[2:None] is s[2:].
Written with AI assistance. Measured on 2026-10-22 on Debian with Python 3.13.5: slicing and indexing a list and str with a float from /, a float literal, a string and None, and the //, int() and None-bound fixes; an __index__ class checked separately. Every block was run verbatim and its output pasted unedited. PlainHeaders is our extension; Python is free.