The encode() method encodes a string in the specified encoding format.
Example
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
str1 = "?!?AaABB"
str1_utf8=str1.encode("UTF-8")
str1_GBK=str1.encode("GBK")
print('UTF-8: ',str1_utf8)
print('GBK: ',str1_GBK)
Output:
UTF-8: b'?!?AaABB'
GBK: b'?!?AaABB'
Syntax
string.encode(encoding='UTF-8',errors='strict')
Parameters
Name | Description |
encoding | The encoding to be used |
errors | Response when encoding fails |
Return Value
It returns the encoded string.