Python | bytes () method

Python Methods and Functions

Interoperability between different data types is handled with ease in Python. This article aims to demonstrate and handle the mutual conversion of various data types to bytes (), usually useful for encoding schemes. 
byte () converts an object into an immutable byte-represented object of the specified size and data.

Syntax: bytes (src, enc, err)

Parameters:
src: The source object which has to be converted
enc: The encoding required in case object is a string
err: Way to handle error in case the string conversion fails.

Returns:
Byte immutable object consisting of unicode 0-256 characters according to src type.
integer: Returns array of size initialized to null
iterable: Returns array of iterable size with elements equal to iterable elements (0-256)
string: Returns the encoded string acc. to enc and if encoding fails, performs action according
to err specified.
no arguments: Returns array of size 0.

Code # 1: demo byte () for integers, none and iterations

# Python 3 demo code
# processing bytes () over int, iterables, no

 
# integer and iteration initialization

a = 4

lis1 = [ 1 , 2 , 3 , 4  , 5 ]

 
# No argument

print ( "Byte conversion with no arguments:" + str (bytes ())) 

  
# convert to bytes

print ( "The integer conversion results in:"   + str (bytes (a)))

print ( "The iterable conversion results in:"   + str (bytes (lis1)))

Output:

 Byte conversion with no arguments: b'' The integer conversion results in: b'x00x00x00x00' The iterable conversion results in: b'x01x02x03x04x05' 

Behavior of bytes with strings

Bytes take a string as an argument and require an encoding scheme to execute it. The most important aspect of this is error handling in case of encoding failure, some of the error handling schemes defined are:

String Error Handlers:
strict: Raises the default UnicodeDecodeError in case of encode failure.
ignore: Ignores the unencodable character and encodes the remaining string.
replace: Replaces the unencodable character with a '?'.

Code # 2: demo bytes () using string

# Python 3 demo code
# processing bytes () above the line

 
# initialization string

str1 = ' GeeksfÖrGeeks'

 
# Provide ascii encoding and ignore the error

print ( "Byte conversion with ignore error: " +

  str (bytes (str1, 'ascii' , errors = 'ignore' ))) 

 
# Give ascii encoding and replace the error

print ( "Byte conversion with replace error:" +

str (bytes (str1, ' ascii' , errors = 'replace' ))) 

  
# Provide ascii coding and hard error
# throws an exception

print ( "Byte conversion with strict error:" +

str (bytes (str1, ' ascii' , errors = 'strict' ))) 

Output:

 Byte conversion with ignore error: b'GeeksfrGeeks' Byte conversion with replace error: b'G eeksf? rGeeks' 

Exception:

 Traceback (most recent call last): File "/home/0458f7fae57c6f4102366356593842ef.py", line 15, in print ("Byte conversion with strict error : "+ str (bytes (str1,' ascii', errors = 'strict'))) UnicodeEncodeError:' ascii' codec can't encode character 'xd6' in position 6: ordinal not in range (128) 




Get Solution for free from DataCamp guru