Search for a string (substring) in the string — it is an application that is used many times in daily life. Python offers this with the index () function, which returns the position of the first occurrence of a substring in a string
Syntax: ch.index (ch1, begp, endp) Parameters: ch1: The string to be searched for. begp (default: 0): This function specifies the position from where search has to be started. endp (default: string_len): This function specifies the position from where search has to end. Return Value: Returns the first position of the substring found. Exception: Raises ValueError if argument string is not found.
|
Output:
The first position of geeks after 2nd index : 8
Exceptions
ValueError: This error occurs when the argument string is not found in the target string.
|
Runtime Error:
Traceback (most recent call last): File "/home/aa5904420c1c3aa072ede56ead7e26ab.py", line 12, in pos = ch.index (ch1) ValueError: substring not found
Practical Application: This function is used to extract the suffix or prefix length after or before the target word . The example below shows the total command length in bits derived from AC voltage given the information in the line.
|
Exit :
The total bit length of AC is: 13