Python | bytes() method

Interconversion between different data types is provided by python language with ease. This article aims at demonstration and working of an interconversion of different data types to bytes(), usually useful for encoding schemes.
byte() converts an object to immutable byte represented object of given 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 : demonstrating byte() on integers, none and iterables

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 code to demonstrate the 
# working of bytes() on int, iterables, none
  
# initializing integer and iterables
a = 4
lis1 = [1, 2, 3, 4, 5]
  
# No argument case
print ("Byte conversion with no arguments : " + str(bytes())) 
  
# conversion to bytes 
print ("The integer conversion results in : "  + str(bytes(a)))
print ("The iterable conversion results in : "  + str(bytes(lis1)))

chevron_right


Output:

Byte conversion with no arguments : b''
The integer conversion results in : b'\x00\x00\x00\x00'
The iterable conversion results in : b'\x01\x02\x03\x04\x05'

Behaviour of Bytes with Strings



Bytes accept string as argument and requires an encoding scheme with it to perform it. Most important aspect of this is handling errors in case of encode failure, some of 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 : demonstration of bytes() using string

filter_none

edit
close

play_arrow

link
brightness_4
code

# Python 3 code to demonstrate the 
# working of bytes() on string
  
# initializing string
str1 = 'GeeksfÖrGeeks'
  
# Giving ascii encoding and ignore error
print ("Byte conversion with ignore error : " +
      str(bytes(str1, 'ascii', errors = 'ignore'))) 
  
# Giving ascii encoding and replace error
print ("Byte conversion with replace error : " +
      str(bytes(str1, 'ascii', errors = 'replace'))) 
  
# Giving ascii encoding and strict error
# throws exception
print ("Byte conversion with strict error : " +
      str(bytes(str1, 'ascii', errors = 'strict'))) 

chevron_right


Output:

Byte conversion with ignore error : b'GeeksfrGeeks'
Byte conversion with replace error : b'Geeksf?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)



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.