Method signature:
replace(old, new, count=-1)
Parameters:
old - The subsequences that is to be found and replaced.
new - The subsequence with which the found subsequences to be replaced.
count - Number of replacements to be performed.
Return value:
The new bytearray object with the find and replacements in-place.
Overview:

- The replace() method of bytearray does a find and replace for a given bytearray obejct and returns the new bytearray. The “find” subsequences are replaced with the “replace” subsequences in the new bytearray.
- A count can be specified to do find and replace only the “count” number of times. Further occurrences of the "find" subsequences are retained without replacement.
Example 1 - Perform find and replace on a Python bytearray:
|
# Example Python program that uses the bytearray method # Create a byte sequence find = b"I" # Find and replace # Print the new byte sequence |
Output:
| bytearray(b'you wish you could manage to be glad') |
Example 2 - Replace old with new on a bytearray for count number of times:
|
# Example Python program that does # Create a byte array old = "pleasant" # Replace old with new byte sequence |
Output:
| A nice walk, a pleasant talk |