Byte Objects and String in Python

Byte Objects and String in Python

The Byte Objects and String in Python


encoding in python,python base64,one hot encoding python,python unicode to string,python utf8


In python 2, both str and bytes are the same typeByte objects whereas in Python 3 Byte objects,
defined in Python 3 are “sequence of bytes” and similar to “unicode” objects from Python 2

As computers can only store bytes of data, we need to convert various data formats in byte data format. For example images to become bytes, are stored with PNG, JPEG etc. Similarly music is stored as .WAV, .MP3 etc. The software responsible for creating and managing this formats do the task of converting this data to bytes so that they can get stored. In python the byte object is a sequence of byte which is not human readable. But a character string is a sequence of characters which is human readable. A character gets encoded before it is stored in computer as bytes.

encoding in python,python base64,one hot encoding python,python unicode to string,python utf8


Encoding

Before the character string is stored into the disk, it has to be encoded. The function in python to encode the string is encode as shown below. Here we are applying the ASCII encoding.

Lets see an example

print('Codedudle'.encode('ASCII'))  


Output :

 b'Codedudle'


Another example

a = 'CodeDudle'
  
# initialising a byte object 
c = b'CodeDudle'
  
# encoded version of a is stored in d 
# using ASCII mapping 
d = a.encode('ASCII') 
  
# checking if a is converted to bytes or not 
if (d==c): 
    print ("Successfully Encoded") 
else
    print ("Encoding Failed") 


Output :

 Successfully Encoded 


Decoding

When the bytes are read form the disk, to make them human readable then they need to be decoded. In python, we can use the decode() function to convert the encoded bytes into strings.

Lets see how by an example

print(b'CodeDudle'.decode('ASCII'))


Output :

 CodeDudle


An another example

# initialising a String

a = 'CodeDudle'

# initialising a byte object

c = b'CodeDudle'

# using decode() to decode the Byte object

# decoded version of c is stored in d

# using ASCII mapping

d = c.decode('ASCII')

# checking if c is converted to String or not

if (d==a):

     print ("Successfully Decoded")

else

     print ("Decoding Unsuccessful") 



Output :

Successfully Decoded


Following are the key points should be note

  • 1. Strings are sequence of character and Bytes objects are sequence of Bytes.
  • 2. Strings are only in human readable but Bytes are machine readable and can store directly to the disk.
  • 3. Bytes are directly stored on the disk, whereas characters need encoding before getting stored in the disk.



In this article i have mentioned the details of byte objects and string in python.

Next Article : Operators in Python

Recommended Post :




























Post a Comment

0 Comments