Python bytes() Function with Examples

In the previous article, we have discussed Python Program for bool() Function
bytes() Function in Python:

The bytes() function returns an object of type bytes.

The bytes() method returns a bytes object, which is an immutable (cannot be changed) sequence of integers ranging from 0 to 256.

It can either convert objects to bytes objects or create an empty bytes object of the specified size.

The distinction between bytes() and bytearray() is that bytes() return an object that cannot be modified, whereas bytearray() returns a modifiable object.

Syntax:

bytes(x, encoding, error)

Parameters

x: When creating the bytes object, this is the source that will be used.

If it is an integer, it will generate an empty bytes object of the specified size.

If it’s a String, make sure to specify the source’s encoding.

encoding(optional): The string’s encoding.

error(optional): Specifies what should happen if the encoding fails.

Return Value:

The bytes() method returns bytes object with the specified size and initialization parameters.

Examples:

Example1:

Input:

Given number = 6

Output:

The result after applying the bytes() method to the given number 6 = b'\x00\x00\x00\x00\x00\x00'

Example2:

Input:

Given string =  "welcome to Python-programs"

Output:

The result after applying the bytes() method to the given string = b'welcome to Python-programs'

Program for bytes() Function in Python

Method #1: Using Built-in Functions (Static Input)

1) For Numbers

Approach:

  • Give the number as static input and store it in a variable.
  • Apply bytes() method to the given number that returns a bytes object, which is an immutable (cannot be changed) sequence of integers ranging from 0 to 256.
  • Store it in another variable.
  • Print the result after applying the bytes() method to the given number.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 6
# Apply bytes() method to the given number that returns a bytes object,
# which is an immutable (cannot be changed) sequence of integers ranging
# from 0 to 256.
# Store it in another variable.
rslt = bytes(gvn_numb)
# Print the result after applying the bytes() method to the given number.
print("The result after applying the bytes() method to the given number",
      gvn_numb, "= ", rslt)

Output:

The result after applying the bytes() method to the given number 6 = b'\x00\x00\x00\x00\x00\x00'

2) For Strings

Approach:

  • Give the string as static input and store it in a variable.
  • Apply bytes() method to the given string that returns an immutable bytes object with the specified size and data.
  • Store it in another variable.
  • Print the result after applying the bytes() method to the given string.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "welcome to Python-programs"
# Apply bytes() method to the given string that returns an immutable bytes
# object with the specified size and data.
# Store it in another variable.
rslt = bytes(gvn_str, 'utf-8')
# Print the result after applying the bytes() method to the given number.
print("The result after applying the bytes() method to the given string = ", rslt)

Output:

The result after applying the bytes() method to the given string = b'welcome to Python-programs'

Method #2: Using Built-in Functions (User Input)

1) For Numbers

Approach:

  • Give the number as user input using the int(input()) function and store it in a variable.
  • Apply bytes() method to the given number that returns a bytes object, which is an immutable (cannot be changed) sequence of integers ranging from 0 to 256.
  • Store it in another variable.
  • Print the result after applying the bytes() method to the given number.
  • The Exit of the Program.

Below is the implementation:

# Give the number as user input using the int(input()) function and store it in a variable.
gvn_numb = int(input("Enter some random number = "))
# Apply bytes() method to the given number that returns a bytes object,
# which is an immutable (cannot be changed) sequence of integers ranging
# from 0 to 256.
# Store it in another variable.
rslt = bytes(gvn_numb)
# Print the result after applying the bytes() method to the given number.
print("The result after applying the bytes() method to the given number",
      gvn_numb, "= ", rslt)

Output:

Enter some random number = 3
The result after applying the bytes() method to the given number 3 = b'\x00\x00\x00'

2) For Strings

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Apply bytes() method to the given string that returns an immutable bytes object with the specified size and data.
  • Store it in another variable.
  • Print the result after applying the bytes() method to the given string.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Enter some random string = ")
# Apply bytes() method to the given string that returns an immutable bytes
# object with the specified size and data.
# Store it in another variable.
rslt = bytes(gvn_str, 'utf-8')
# Print the result after applying the bytes() method to the given number.
print("The result after applying the bytes() method to the given string = ", rslt)

Output:

Enter some random string = hello this is btechgeeks
The result after applying the bytes() method to the given string = b'hello this is btechgeeks'

Find a Comprehensive Collection of Python Built in Functions that you need to be aware of and use them as a part of your program.