Overview:
• For a given subsequence, the find() method of bytearray class in Python returns the index of its first occurrence.
• When a range of positions is provided by specifying the positions “after” and “before”, the find() method returns the index of the first occurrence within this range.
• When no occurrence of the subsequence is found the find() method returns -1.
Example 1 - Find the position of a subsequence in a bytearray:
|
# Example Python program that finds the # Create a bytearray # The type of subsequence is bytes # Find the first occurence of a substring print("Position of the subsequence in the bytearray:") |
Output:
| Position of the subsequence in the bytearray: 3 |
Example 2 - Find a subsequence in a bytearray between a range of positions:
|
# Example Python program that finds the buffer = bytearray(b"I knew who I was this morning, but I have changed") # Find the position of a subsequence between the positions after # Find the position of a subsequence in the slice[15, len(buffer)] |
Output:
| Subsequence position between index 5 and 15: 11 Position of the subsequence after index 15: 35 |