Signup/Sign In
LAST UPDATED: AUGUST 23, 2019

Python bytearray() Method - Python Library Functions

    In this article, we are gonna see python bytearray() method. This method returns a bytearray object which is a mutable sequence of integers in the range 0 <= x < 256.

    bytearray function in python

    Let's have a look at the syntax of the bytearray() function:

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

    This method takes 3 types of parameters which are totally optional(not compulsory), they are:

    1. source[optional] : Used for initializing the array of bytes. It can be an integer or a string or an iterable or anything else. If you provide string, then in the next parameter, you must provide the necoding too.

    2. encoding[optional] : If the source is a string, then you can provide the encoding of string. For example ascii, utf-8, etc.

    3. errors[Optional] : In case of a string to be converted into bytearray object, if encoding fails, what to do then, it is specified here. Here are some values that you can provide in this parameter:

      1. strict: It will raise an exception if encoding fails.

      2. ignore: Specifying this will ignore the error.

      3. replace: By using this you can specify some replacement string for the data which is causing error.

    The Source[Optional] parameter can be used to initialize the array in a few different ways, let's discuss each one with examples.




    1) For String

    If source parameter is a string, you must provide encoding and error parameters. The bytearray() method converts the string to bytes using str.encode() method.

    str = "Studytonight"
      
    # encoding the string with unicode 7, 8(most popular encoding), 16 and 32 
    b1 = bytearray(str, 'utf-7') 
    b2 = bytearray(str, 'utf-8') 
    b3 = bytearray(str, 'utf-16') 
    b4 = bytearray(str, 'utf-32') 
    
    print(b1) 
    print(b2)
    print(b3)
    print(b4)  
    

    Output:

    bytearray(b'StudyTonight')
    bytearray(b'StudyTonight')
    bytearray(b'\xff\xfeS\x00t\x00u\x00d\x00y\x00T\x00o\x00n\x00i\x00g\x00h\x00t\x00')
    bytearray(b'\xff\xfe\x00\x00S\x00\x00\x00t\x00\x00\x00u\x00\x00\x00d\x00\x00\x00y\x00\x00\x00T\x00\x00\x00o\x00\x00\x00n\x00\x00\x00i\x00\x00\x00g\x00\x00\x00h\x00\x00\x00t\x00\x00\x00')
    
    



    2) For Integer

    If source parameter is a integer, it creates an array of length equal to the integer value and initializes all the array elements as null values.

    # size of array 
    size1 = 3
    size2 = 5
    
    b1 = bytearray(size1)
    b2 = bytearray(size2) 
      
    print(b1)
    print(b2)
    

    Output:

    bytearray(b'\x00\x00\x00')
    bytearray(b'\x00\x00\x00\x00\x00')
    
    



    3) Some Object

    If source parameter is an object conforming to the interface of buffer, a read-only buffer of the object will be used to initialize the bytes array.

    # Creates bytearray from byte literal 
    b1 = bytearray(b'code') 
      
    # iterating the value 
    for value in b1: 
        print(value)
    

    Output:

    99
    111
    100
    101
    
    



    4) For an Iterable

    If source parameter is an Iterable, it must be an iterable of integers in the range 0 <= x < 256.

    list = [1, 4, 3]
      
    b1 = bytearray(list) 
      
    print(b1) 
    print("Number Of bytes:", len(b1)))
    

    Output:

    bytearray(b'\x01\x04\x03')
    Number Of bytes: 3
    
    



    5) For Null Source

    If source parameter is a Null, a Null ByteArray is created.

    b1 = bytearray() 
    print(b1) 
    

    Output:

    bytearray(b'')
    



    Conclusion:

    Pheww! This is it. I hope that you enjoyed the post and learned about the bytearray() python library function. If you feel that this post is useful, please share it with your friends and colleagues.

    Thanks for reading it till the end.

    I am a self-taught Blogger and Programmer. I have good sort of knowledge in Technology, PHP, Javascript, HTML, CSS and Python.
    IF YOU LIKE IT, THEN SHARE IT
    Advertisement

    RELATED POSTS