How to Use bytes() Function in PythonPython Basics


The bytes() method returns an immutable byte object initialized with the given size and data.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

print(bytes([11,12,13,14]))
print(bytes('ABCCBA', 'utf-8'))
print(bytes())

Output:

b'x0bx0crx0e'
b'ABCCBA'
b''

Syntax

bytes([source[, encoding[, errors]]])

Parameters

Name Description
source Source to initialize the array of bytes.
encoding If source is a string, the encoding of the string.
errors If source is a string, the action to take when the encoding conversion fails.

Return Value

It returns a bytes object of the given size and data.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us