Python Tutorial - Bytes Type and Usage

Comparison between Bytes and Strings:

  • A character string is composed of several characters and is operated in character units; a byte string is composed of several bytes and is operated in byte units.
  • Byte string and strings are basically all the same except that they operate on different data units.
  • Byte strings and strings are immutable sequences. You cannot add or delete data as you wish.

Byte is only responsible for storing data in the form of byte sequences (binary form). In simple explanation, bytes just simply record the raw data in memory. As how to use these data, bytes do not care, and you can use it as you want.

Therefore, Bytes type of data is very suitable for internet transmission and can be used for network communication programming. Bytes can also be used to store pictures, audio, video and other binary format files.

Strings and bytes have closed relationship with each other. You can use strings to create bytes objects or convert strings to bytes objects. There are three ways to achieve this:

a. If the content of the string are all ASCII characters, you can convert them to bytes directly by prefixing the string with alphabet b.

b. You can call its constructor, bytes() in order to convert a string into bytes according to a specified character set. If no character set is specified. UTF-8 is used by default.

c. The string itself has an encode() method, which is specifically used to convert the string into the corresponding byte string according to the specified character set. If no character set is specified. UTF-8 is used by default.

Example:

a1 = bytes ()       #Create empty bytes by constructor
a2 = b '   '        #Create empty bytes by empty string
a3 = b'http://www.freelearningpoints.com/' #Convert string to bytes by b prefix
print ("a3:", a3)
print (a3[3])
print (a3[7:22])
#Specify the character set for the bytes() method
a4 = bytes ('Welcome to Free Learning Points', encoding = 'UTF-8')
print ("a4:", a4)
#Convert string to bytes by encode() method
a5 = "Welcome to Free Learning Points" .encode ('UTF-8')
print ("a5:", a5)

The output is:

a3: b'http://www.freelearningpoints.com/'
112
b'www.freelearnin'
a4: b'Welcome to Free Learning Points'
a5: b'Welcome to Free Learning Points'

From the results, you can find that for non-ASCII characters, print its character encoding value (hexadecimal format), not the character itself. Non-ASCII characters generally occupy more than two bytes of memory, while bytes process data as a single byte, thus multiple bytes cannot be processed at once.

                               


More Tutorials:

Python Installation - Linux (Ubuntu)
Python Installation - Mac OS
Integrated Development Environment - IDE
Python - Basic Variables
Python - Sequence Introduction
Python - Output Formatting
Python - Escape Character
Python - Type Conversion
Python - Numbers
Python – Arithmetic Operators
Python – Assignment Operators
Python – Comparison Operators
Python – Logical Operators
Python – Precedence and Associativity Operators
Python – Bytes Type and Usage
Python – Long & Raw Strings
Python – Concatenate Function
Python – Slice Function
Python – Length and Split Function
Python – Join and Count Function
Python – Find Function
Python – Index Function
Python – Alignment Function
Python – Startswith and Endswith Function
Python – String Case Conversion
Python – Remove Specified Character
Python – Encode and Decode Function
Python – dir and help Function
Python – Input Output Introduction
Python – Basic Operation
Python – Open Function
Python – Read Function
Python – Readline Function
Python – Write Function
Python – Close Function
Python – Seek and Tell Function
Python – Pickle Module
Python - File Input Module and Linecache Module
Python - Pathlib Module
Python - Pathlib Module
Python - os.path Module
Python - fnmatch Module
Python - Tuple Introduction
Python - List Introduction
Python - List Add Element
Python - List Delete Element
Python - List Modification Element
Python - List Find Element
Python - Dictionary Introduction
Python - Dictionary Basic Operation
Python - Dictionary Method Guide
Python - Set Collection
Python - Set Basic Operation
Python - Set Basic Method
Python - Frozenset Method
Python - If Condition I
Python - If Condition II
Python - While loop
Python - For loop
Python - Pass Statement
Python - Break Statement
Python - Zip Reverse Function
Python - Function Introduction
Python - Positional Parameters
Python - Key Arguments
Python - None and Return
Python - Variable Scope
Python - Local Function
Python - Closure Method
Python - Lamdba Expression


More Python Exercises:

Python String Exercises
Python List Exercises
Python Library Exercises
Python Sets Exercises
Python Array Exercises
Python Condition Statement Exercises