1. The Python encode() method

The Python string encode() method is used to encode the string using the provided encoding. This function returns the bytes object. If we don't provide an encoding, "utf-8" encoding is used by default. 2. The Python Bytes decode() methodThe Python bytes decode() function is used to convert bytes to a string object.

Syntax & Example

Syntax

string.encode(encoding=encoding, errors=errors)
encodeBytes.decode()

Example

my_string = 'Python'

encodeBytes = my_string.encode(encoding='utf-8')
print(encodeBytes)
print(type(encodeBytes))

my_string_decode = encodeBytes.decode()
print(type(my_string_decode))

print('Encoded bytes =', encodeBytes)
print('Decoded String =', my_string_decode)
print('my_string equals my_string_decode =', my_string == my_string_decode)
"""
output:
b'Python'


Encoded bytes = b'Python'
Decoded String = Python
my_string equals my_string_decode = True
"""

Younes Derfoufi
my-courses.net

Leave a Reply