Python encode string to bytes – How to convert String to Bytes in Python?

Python encode string to bytes: In this post, we’ll look at how to convert Python String to Bytes and Python Bytes to String. Python type conversion has grown in popularity due to its feature of data used in different forms during various operations.

Python conversion of String to bytes and bytes to String is important because it is required when working with files, etc.

Bytes are simply encoded objects of string that can be stored on a disk and returned to their original form by decoding them to string.

Strings and bytes vary within this string are a sequence of characters, whereas byte objects are an arrangement of bytes.

Converting String to Bytes in Python

Python convert string to byte: There are several methods for converting a string to bytes.

Method #1: Using bytes() method

Python convert str to bytes: The bytes() method in Python’s CPython library allows us to convert String to bytes.

It should be noted that the UTF-8 format is utilized for encoding.

Syntax:

bytes(string, 'utf-8')

Approach:

  • Give the sting as static input and store it in a variable.
  • Print the given string
  • Print the datatype of the given string by passing it as an argument to the type() function.
  • Pass the given string, ‘UTF-8’ as arguments to the bytes() function to convert the given string into bytes and store it in another variable.
  • Here UTF-8 format is used for encoding.
  • Print the given string after converting it into bytes
  • Print the datatype of the above variable i.e, after conversion by passing it as an argument to the type() function.
  • The Exit of the Program.

Below is the implementation:

# Give the sting as static input and store it in a variable.
gvn_str = 'Hello this is btechgeeks' 
# Print the given string
print("The given string:\n", gvn_str)
# Print the datatype of the given string by passing it as an argument 
# to the type() function.
print("datatype:", type(gvn_str))
print()
# Pass the given string, 'UTF-8' as arguments to the bytes() function 
# to convert the given string into bytes and store it in another variable.
# Here UTF-8 format is used for encoding.
byte_str = bytes(gvn_str,'UTF-8')
# Print the given string after converting into bytes
print("The given string after converting into bytes:")
print(byte_str)
# Print the datatype of the above variable by passing it as an argument 
# to the type() function.
print("datatype after conversion:", type(byte_str))

Output:

The given string:
Hello this is btechgeeks
datatype: <class 'str'>

The given string after converting into bytes:
b'Hello this is btechgeeks'
datatype after conversion: <class 'bytes'>

Method #2: Using encode() method

Approach:

  • Give the sting as static input and store it in a variable.
  • Print the given string
  • Print the datatype of the given string by passing it as an argument to the type() function.
  • Apply encode() function on the given string by passing ‘UTF-8’ as an argument to it to convert the given string into bytes and store it in another variable.
  • Here UTF-8 format is used for encoding.
  • Print the given string after converting into bytes
  • Print the datatype of the above variable by passing it as an argument to the type() function.
  • The Exit of the Program.

Below is the implementation:

# Give the sting as static input and store it in a variable.
gvn_str = 'Hello this is btechgeeks' 
# Print the given string
print("The given string:\n", gvn_str)
# Print the datatype of the given string by passing it as an argument 
# to the type() function.
print("datatype:", type(gvn_str))
print()
# Apply encode() function on the given string by passing 'UTF-8' as argument to it  
# to convert the given string into bytes and store it in another variable.
# Here UTF-8 format is used for encoding.
byte_str = gvn_str.encode('UTF-8')
# Print the given string after converting into bytes
print("The given string after converting into bytes:")
print(byte_str)
# Print the datatype of the above variable by passing it as an argument 
# to the type() function.
print("datatype after conversion:", type(byte_str))

Output:

The given string:
Hello this is btechgeeks
datatype: <class 'str'>

The given string after converting into bytes:
b'Hello this is btechgeeks'
datatype after conversion: <class 'bytes'>

Leave a Comment