How to Use String encode() Method in PythonPython Basics


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.

© 2024 Cheerlearn.com, all rights reserved. Privacy Policy | Contact Us