Cheap Jerseys Cheap NFL Jerseys Cheap NFL Jerseys Cheap Football jerseys Cheap NFL Jerseys Wholesale Jerseys Cheap NHL Jerseys Cheap Jerseys From China Cheap MLB Jerseys Cheap Jerseys Cheap Jerseys Cheap Jerseys Wholesale Jerseys Python String encode() Method - WTMatter

Python String encode() Method

Python String Encode() Method

This is a tutorial of the Python String encode() method. Learn to get the encoded versions (UTF-8, ASCII, etc.) of a given string with the help of examples.

Python String encode()

This method returns the specified encoding of a given Python String. You can make use of this method to convert a given string into its different encodings such as the UTF-8, ASCII, etc.

From Python Version 3.0 onwards, the strings are by default stored in Unicode Encoding. In this type of encoding, every character is represented by a particular code. Therefore, a Unicode string is basically a sequence of these codes.

To find out more about these codes and to find out the code of a particular string character and vice-versa, it is recommended to know about the following built-in functions.

UTF-8 and ASCII are the two most commonly used encodings. But Python supports a lot of different encodings. Find out all of the supported encodings here.

Syntax

Given below is the syntax for the str.encode() method.

encode() Parameters

This method can take upto two arguments that are described below.

encoding. It is the encoding in which you want to get the string on which you are applying this method. The default value of this parameter is set to 'UTF-8'.

errors. This specifies how this function should behave when errors are encountered. The default value of this parameter is 'strict'. It can take six different values that are described below.

  • strict. UnicodeDecodeError exception will be raised if this method encounters any type of error.
  • ignore. It will ignore the uncodeable Unicode from the string.
  • replace. The unencodable Unicode will be replaced with the question mark symbol.
  • xmlcharrefreplace. The Unencodable Unicode will be replaced by XML character reference.
  • backslashreplace. The \uNNNN escape sequence will be substituted in place of the Unencodable Unicode.
  • namereplace. The \N{...} escape sequence will be substituted in place of the Unencodable Unicode.

encode() Return Value

It returns the given string in the encoding specified in the encoding argument of this method. The original string remains unchanged.

Examples

Given below are the two examples demonstrating the usage of the Python String encode() method.

Example 1. Encode a Given String into UTF-8 Encoding

As already mentioned, Python by default stores the strings in Unicode encoding. Therefore, in this example, we’ll be converting a string from Unicode to UTF-8 using the str.encode() method.

#Sample String
#By Default Stored in Unicode
name = "English - Gurmeet Singh, हिन्दी: गुरमीत सिंह"

#Printing Unicode String
print("Unicode String:", name)

#Getting the Unicode String Encoded in UTF-8
#No Need to specify the encoding argument as its default value is UTF-8
utf8_name = name.encode()

#Printing name in UTF-8
print("UTF-8 String:", utf8_name)

Output.

Unicode String: English - Gurmeet Singh, हिन्दी: गुरमीत सिंह
UTF-8 String: b'English - Gurmeet Singh, \xe0\xa4\xb9\xe0\xa4\xbf\xe0\xa4\xa8\xe0\xa5\x8d\xe0\xa4\xa6\xe0\xa5\x80: \xe0\xa4\x97\xe0\xa5\x81\xe0\xa4\xb0\xe0\xa4\xae\xe0\xa5\x80\xe0\xa4\xa4 \xe0\xa4\xb8\xe0\xa4\xbf\xe0\xa4\x82\xe0\xa4\xb9'

Similarly, you can get a given string in numerous other encodings.

Example 2. Encoding a String with the different values of the errors argument

Here we’re converting the given string into ASCII encoding but with all the six different values for the errors argument. Here we’re only using the ASCII encoding for the conversion in all the cases, but you can experiment to see the results for different types of encoding and errors argument values.

#Unicode String
amount = "₹ 500"

#Printing Unicode String
print("Unicode String:", amount)

#ignore errors
print("Ignore Errors:", amount.encode("ascii", "ignore"))

#replace errors
print("Replace Errors:", amount.encode("ascii", "replace"))

#xmlcharrefreplace errors
print("xmlcharrefreplace Errors:", amount.encode("ascii", "xmlcharrefreplace"))

#backslashreplace errors
print("backslashreplace Errors:", amount.encode("ascii", "backslashreplace"))

#namereplace errors
print("namereplace Errors:", amount.encode("ascii", "namereplace"))

#strict errors
print("Strict Errors:", amount.encode("ascii"))

Output.

Unicode String: ₹ 500
Ignore Errors: b' 500'
Replace Errors: b'? 500'
xmlcharrefreplace Errors: b'₹ 500'
backslashreplace Errors: b'\\u20b9 500'
namereplace Errors: b'\\N{INDIAN RUPEE SIGN} 500'
Traceback (most recent call last):
  File "<stdin>", line 23, in <module>
    print("Strict Errors:", amount.encode("ascii"))
UnicodeEncodeError: 'ascii' codec can't encode character '\u20b9' in position 0: ordinal not in range(128)

I hope you found this guide useful. If so, do share it with others who are willing to learn Python and other programming languages. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

Your email address will not be published. Required fields are marked *