Bytes() method in Python Built in Function
bytes method in built function returns the bytes as an object in the given function.This function is an immutable sequence of integers in the range of 0<=x<=256.
Syntax:
bytes(([source[, encoding[, errors]]]))
Parameters:
Bytes method has 3 parameters. These parameters are optional
- Source: this is used to initialize the source of an array
- Encoding: if the source is a string then this is used as encoding of the string
- Errors: if the source is string, this parameter takes the action when the encoding conversion fails.
Table for different source parameters
| Type of source | Description |
| String | This converts the string into bytes by using str.encode(). We have to provide encode and errors |
| Integer | This creates an array of provided size |
| Object | To initialize the byte array read only buffer object is used |
| Iterable | It creates an array whose size is equal to the Iterable count |
| No source(arguments) | It creates an array with size 0 |
Example-1:
Byte method using integer
x = bytes(2) print(x)
Output:

Example-2:
Conversion of string into bytes
x = "There are many colours in the Universe" text= bytes( x ,'utf-8') print(text)
Output:

Example-3:
Converting list into bytes
text = [4,5,6,7,8] x= bytes( text) print(x)
Output:
