The encode() method returns an encoded string. Strings preserved as Unicode after Python 3.0. python encode utf-8 use if no encoding is defined.
The syntax of the method is: str.encode(encoding=’UTF-8′,errors=’strict’)
Below is the python program to demonstrate the encode() function:
# Python program to demonstrate the # use of encode() function import base64 string = 'Technology Motivator' data = base64.b64encode(string.encode()) print(data)
When we run above the program, the outcome is as follows:
Output : b'VGVjaG5vbG9neSBNb3RpdmF0b3I='
The decode() method decode utf-8 the string by utilizing the encoding codec. It works opposite to the encode.
The syntax of the method is: str.decode(encoding='UTF-8',errors='strict')
Below is the python program to demonstrate the decode() function:
# Python program to demonstrate the # use of decode() function import base64 data = b'VGVjaG5vbG9neSBNb3RpdmF0b3I=' data = base64.b64decode(data.decode()) print(data)
When we run above the program, the outcome is as follows:
b'Technology Motivator'
Below are several other functions that we can use to work with string in Python 3