Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / Python

str.index

Returns the lowest index at which the substring occurs in the search interval.

Version: Python 3.14; repository image, production runtime not attested

Syntax

s.index(sub[, start[, end]])

Returns the lowest index at which the substring occurs in the search interval.

An absent substring raises ValueError, allowing the caller to distinguish a missing match from any valid index.

The optional start and end bounds restrict the search and exclude the end position. In cacao, starting at one skips the first ca and finds the next occurrence at two. Searching for a missing z triggers the exception handler.

Examples

print("cacao".index("ca", 1))
try:
    "cacao".index("z")
except ValueError as error:
    print(type(error).__name__)

Result

2
ValueError

The optional start and end bounds restrict the search and exclude the end position. In cacao, starting at one skips the first ca and finds the next occurrence at two. Searching for a missing z triggers the exception handler.

Sources