Python String encode()
Python string encode() function is used to encode the string using the provided encoding. This function returns the bytes object. If we don’t provide encoding, “utf-8” encoding is used as default.
Python Bytes decode()
Python bytes decode() function is used to convert bytes to string object. Both these functions allow us to specify the error handling scheme to use for encoding/decoding errors. The default is ‘strict’ meaning that encoding errors raise a UnicodeEncodeError. Some other possible values are ‘ignore’, ‘replace’ and ‘xmlcharrefreplace’. Let’s look at a simple example of python string encode() decode() functions.
str_original = 'Hello' bytes_encoded = str_original.encode(encoding='utf-8') print(type(bytes_encoded)) str_decoded = bytes_encoded.decode() print(type(str_decoded)) print('Encoded bytes =', bytes_encoded) print('Decoded String =', str_decoded) print('str_original equals str_decoded =', str_original == str_decoded)Output:
<class 'bytes'> <class 'str'> Encoded bytes = b'Hello' Decoded String = Hello str_original equals str_decoded = TrueAbove example doesn’t clearly demonstrate the use of encoding. Let’s look at another example where we will get inputs from the user and then encode it. We will have some special characters in the input string entered by the user.
str_original = input('Please enter string data:\n') bytes_encoded = str_original.encode() str_decoded = bytes_encoded.decode() print('Encoded bytes =', bytes_encoded) print('Decoded String =', str_decoded) print('str_original equals str_decoded =', str_original == str_decoded)Output:
You can checkout complete python script and more Python examples from our GitHub Repository.
Reference: str.encode() API Doc, bytes.decode() API Doc
Want to learn more? Join the DigitalOcean Community!
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.
Sign up
Description
Python string method decode() decodes the string using the codec registered for encoding. It defaults to the default string encoding.
Syntax
Str.decode(encoding='UTF-8',errors='strict')Parameters
encoding − This is the encodings to be used. For a list of all encoding schemes please visit: Standard Encodings.
errors − This may be given to set a different error handling scheme. The default for errors is 'strict', meaning that encoding errors raise a UnicodeError. Other possible values are 'ignore', 'replace', 'xmlcharrefreplace', 'backslashreplace' and any other name registered via codecs.register_error().
Return Value
Decoded string.
Example
#!/usr/bin/python Str = "this is string example....wow!!!"; Str = Str.encode('base64','strict'); print "Encoded String: " + Str print "Decoded String: " + Str.decode('base64','strict')Result
Encoded String: dGhpcyBpcyBzdHJpbmcgZXhhbXBsZS4uLi53b3chISE= Decoded String: this is string example....wow!!!python_strings.htm
To represent a unicode string as a string of bytes is known as encoding. To convert a string of bytes to a unicode string is known as decoding. You typically encode a unicode string whenever you need to use it for IO, for instance transfer it over the network, or save it to a disk file. You typically decode a string of bytes whenever you receive string data from the network or from a disk file.
To encode a string using a given encoding you can do the following:
>>>u'æøå'.encode('utf8') '\xc3\x83\xc2\xa6\xc3\x83\xc2\xb8\xc3\x83\xc2\xa5'To decode astring(using the same encoding used to encode it), you need to call decode(encoding). For example:
>>>'\xc3\x83\xc2\xa6\xc3\x83\xc2\xb8\xc3\x83\xc2\xa5'.decode('utf8') u'\xc3\xa6\xc3\xb8\xc3\xa5'This string in utf8 encoding is equivalent to u'æøå'
Updated on 30-Sep-2019 07:32:24
- Related Questions & Answers
- Encode and decode uuencode files using Python
- Encode and Decode Strings in C++
- Encode and decode binhex4 files using Python (binhex)
- Encode and decode XDR data using Python xdrlib
- Arduino – base64 encode and decode
- Encode and decode MIME quoted-printable data using Python
- How to encode and decode a URL in JavaScript?
- How to Encode and Decode JSON and Lua Programming?
- What is the difference between = and == operators in Python?
- What is the difference between attributes and properties in python?
- What is the difference between os.open and os.fdopen in python?
- What is the difference between dict.items() and dict.iteritems() in Python?
- What is the difference between __str__ and __repr__ in Python?
- What is the difference between semicolons in JavaScript and in Python?
- What is the difference between Python functions datetime.now() and datetime.today()?