Q:

Write a program in Python to encode and decode the following string

0

Write a program in Python to encode and decode the following string

Hello Friend! Welcome to this tutorial.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Solution

Python encode()

Python has predefined encode() method to encode the string. It comes with a number of built in codecs, like - ascii, big5, latin_1, utf_7, utf_8 etc. If no any encoding codec specified, by default UTF-8 will be used.

Syntax of Python Encoding
string.encode(encoding, errors) 

Here, both encoding and errors are optional parameters. The encoding parameter by default take UTF-8.

Python decode()

Python has predefined decode() method to decode the string. This works opposite of encode. It accepts the encoding of the encoded string to decode it and returns the original string.

This is the following solution to encode and decode the given string -

str = "Hello Friend! Welcome to this tutorial."
 
# string encoding 
str_enc = str.encode('base64', 'strict')
 
print "The encoded string is : ",
print str_enc
 
# string decoded
print "The decoded string is : ",
print str_enc.decode('base64', 'strict')
Output of the above code

The encoded string is : SGVsbG8gRnJpZW5K15b211HRvIHRoXMghdhV0b3JPwu

The decoded string is : Hello Friend! Welcome to this tutorial.

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python Exercises, Practice Questions and Solutions

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Write a program in Python to read content from one... >>
<< Write a program in Python to replace a, b with 1, ...