Python String index() Method

21-02-23 Ahmed Obaid 1621 0

​The index() function returns the index number of the first occurrence of a substring (plain text) in the specified string (case sensitive). It's the same as the find() function except that if the substring is not found, it raises a ValueError if the substring is not found or if the index is outside the scope of the search.


How to formulate it like this:

string. index(substring, start, end)

Parameters:

substring : (required) A substring that must be searched for in the given string.

start ( optional ) : ( Optional ) The starting position where the sub-string within the string needs to be checked. The default is 0.

End ( ) : ( Optional ) The index of the end within the string that the search ends with. default is the end of string,

Return value: The index() function returns an integer of the index number of the first occurrence of a substring (plain text) in the specified string.

Errors and exceptions:

If the substring is not found or if the index is outside the scope of the search.


We will use the index() function without the optional ( start ) and ( end ) parameters.undefined We'll just use the substring parameter to find the index number of the first occurrence of a substring in the given string.

 In the following example, we will search for the index number of the first occurrence of the substring represented by the word (obaid),

 Example:

 # The string to be searched
 string = "learn python With ahmed obaid"
 # Searches the given string for the index number of the first occurrence of the substring and returns an integer
 print(string. index("obaid")

 The output will be:

 24

 In the following example, we will use the index() function with optional (start) and (end) parameters.

 Example

# The string to be searched
string = "learn python With ahmed obaid"
# Searches the given string for the index number of the last occurrence of the substring and returns an integer
# Noting that we started the search from index #2
print(string. index("py", 2))
# Searches the given string for the index number of the last occurrence of the substring and returns an integer
# Noting that we started the search process from index number 0 to the end index number 10
print(string. index("learn", 0, 10))
# Searches the given string for the index number of the last occurrence of the substring and returns an integer
# Noting that we started the search process from index number 2 to the end index number 30
print(string. index("obaid", 10, 30))

The output will be:

6
0
24

 We will throw a ValueError if the substring is not found or if the index is outside the scope of the search.

 Example:

 # The string to be searched
 string = "learn python With ahmed obaid"
 # If the substring is not found or the index is outside the scope of the search
 # We will get an error exception
 print(string. index("Hello")

 The output will be: 

Traceback (most recent call last): File "/usr/lib/python3.8/py_compile.py", line 144, in compile code = loader. source_to_code(source_bytes, dfile or file, File "", line 846, in source_to_code File "", line 219, in _call_with_frames_removed File "./prog.py", line 5 print(string.index("Hello") ^
 SyntaxError: unexpected EOF while parsing

 During handling of the above exception, another exception undefined occurred:

 Traceback (most recent call last): File "", line 1, in File "/usr/lib/python3.8/py_compile.py", line 150, in compile raise py_exc
 py_compile.PyCompileError: File "./prog.py", line 5 print(string.index("Hello") ^
 SyntaxError: unexpected EOF while parsing

 External sources:

 Built-in functions - official Python documentation

 If you have any questions or concerns, leave them in the comments



Tags


Python python data types Python String string methods python String search functions in Python python string index method

Share page