Welcome to our comprehensive guide on Encoding and Decoding Strings! This lesson is designed to help both beginners and intermediate learners understand the art of manipulating data in a fun and engaging way. š”
Encoded strings are text representations of data that have been altered to make them more compact, secure, or convenient for specific purposes. Decoded strings, on the other hand, are the original, unaltered text versions of the encoded data.
Encoding and Decoding can be useful in various scenarios such as:
Base64 is a group of binary-to-text encoding schemes that represent binary data in an ASCII string format. It's commonly used for email attachments, HTTP headers, and in JSON web tokens (JWT).
import base64
message = "Hello, World!"
encoded_message = base64.b64encode(message.encode()).decode()
print(encoded_message)š Note: The encode() function converts the string to bytes, which is necessary for the base64.b64encode() function to work.
encoded_message = "SGVsbG8sIFdvcmxkIQ=="
decoded_message = base64.b64decode(encoded_message).decode()
print(decoded_message)URL encoding, also known as percent encoding, is a method to encode information in a Uniform Resource Identifier (URI). It is used when a URI needs to contain characters that are not allowed in a URI.
import urllib.parse
message = "Hello, World! With spaces & symbols: !@#$%"
encoded_message = urllib.parse.quote(message)
print(encoded_message)š Note: The urllib.parse.quote() function encodes the spaces and special characters in the message, replacing them with their percent-encoded equivalents.
encoded_message = "SGVsbG8sIFdvcmxkIQ==%21%40%23%24%25"
decoded_message = urllib.parse.unquote(encoded_message)
print(decoded_message)š Note: The urllib.parse.unquote() function decodes the percent-encoded characters back to their original values.
What is Base64 encoding used for?
Which function in Python is used for URL encoding?