String Methods
str.index()
Locate the first occurrence of a substring entirely within the requested bounds. Return an absolute code-point offset in the original string, not an offset from start. Raise ValueError for a missing match. Empty substrings match boundaries, but a start beyond the original length does not match. The original string is unchanged.
Formal Syntax & Structure
python syntaxpython
text.index(sub[, start[, end]])Parameters & Attributes
| Name | Type | Description |
|---|---|---|
| sub | str | Substring to locate or count. |
| start | int | None | Optional inclusive start bound; negative values are relative to the end. |
| end | int | None | Optional exclusive end bound; defaults to the end. |
Return Value
int
Absolute matching index (raises ValueError if absent).
Working Example
Example Snippetpython
path = "item/item"print(path.index("item"))print(path.index("item", 1))try: path.index("missing")except ValueError: print("not found")