A byte is a combination of eight bits while a string consists of a characters and a combination of these characters makes words and then sentences. These are easily comprehended by a human. However, a machine can only understand the binary information. To convert the data into the binary format, the b keyword is used. This conversion is known as encoding while converting the binary data back to the textual information is called decoding.
This article discusses the functionality of the ‘b’ character in detail and discusses it with different examples.
What Does the ‘b’ Character do in Front of a String Literal?
The b character before a string means that the string is in the byte format. The string is now a literal that contains a series of bytes instead of characters. Byte literals contain visual, auditory, or any other type of information in the binary format. The binary format is also called encoded format which can be read by a machine. The conversion of the encoded data back to the human-readable format is called decoding. The following diagram represents the difference between encoding and decoding:
Let us look at various examples that uses the b character to convert a string into bytes format.
Example 1: Using the b Character to Convert the String to the Byte Format
The following example uses the b character before the string to convert it into the bytes format. The following code explains how to convert the string data into bytes:
byte_data = b'STRING DATA'
print(byte_data)
print(type(byte_data))
In the above code,
- The b character is added before the input string that converts it into the bytes format. The result is stored in the byte_data which is then printed.
- The type of the byte_data is printed using the type() method. The type of the byte_data is bytes.
Output
The following output displays the bytes literal ‘STRING DATA’ and its type which is bytes:
Example 2: How to Differentiate Between a Simple String and a Byte String?
A string is a sequence of characters that are readable by a human. However, the byte strings contain data in the encoded format. Strings must be converted to binary data to be read by the computer. Using the byte strings, this conversion is not needed as they are in the encoded format. The following code explains the difference between a normal string and a byte string:
text = "Coding in Python is fun!"
print("The input string is: ", text)
print("The type of the string is: ", type(text),"\n")
byte_str = b"Coding in Python is fun!"
print("The input string is: ", byte_str)
print("The type of the string is: ", type(byte_str))
In the above code,
- A string text is defined which says “Coding in Python is fun!”. The string and its type are printed using the type() function
- The b keyword is added before the same string that converts it into the format of the byte. The converted string is stored in the variable byte_str. The byte string is printed along with its type.
Output
The following output displays the difference between a simple string and a byte string:
Example 3: Getting to Know About the ASCII Equivalent of a Character Using chr() and ord() Methods
ASCII is known as the American standard code for the information interchange. It is an efficient method to encode the individualized characters. These are decimal equivalents of the binary data of each character. To know about the ASCII value of a character, the Python methods ord() and chr() can be used. The following code explains how to get the ASCII values of each character using ord() and chr():
text= "CODE"
def ASCII_eq(string):
for char in text:
print(f"The ACII value of the character {char} is:", ord(char))
ASCII_eq(text)
print('\n')
list1=[345,56,78,86]
def char_eq(ASCII_list):
for i in ASCII_list:
print(f"The character corresponding to the ASCII value {i} is:", chr(i))
char_eq(list1)
In the above code,
- A sample string text is defined which says “CODE”.
- A function ASCII_eq is defined which takes the parameter value of the string. The function converts each character value to its ASCII equivalent.
- It iterates through each character in the string value and prints its corresponding ASCII value using the ord() method in Python.
- The function is called on the string text.
- Another function char_eq is defined which takes the parameter ASII_list and checks for each value in the list and returns its character equivalent using the Python method chr().
- The method is called on the list1 which contains different ASCII values. Each ASCII value is converted to its corresponding character.
Output
The following output displays how a character and its equivalent ASCII values can be accessed through the ord() and chr() methods in Python:
Example 4: Encoding/Conversion to Bytes
Encoding is a process where the information is converted into the binary format that is bytes. Converting the string data to bytes is also classified as encoding. Different encoding systems exist that convert the string data into bytes which are ASCII, UTF-16, and more. Let us look at the following code that explains the encoding method using the encoding systems:
byte_str = "I love to code!".encode('UTF-8')
print("The converted string is:", byte_str)
print("The type of the converted data is:",type(byte_str))
In the above code,
- The encode method with the parameter value of UTF-8 is called on the string data “I love to code!” which converts the string to the byte format. The encoded data is stored in the variable byte_str.
- The converted byte string is printed along with its type.
Output
The following output displays the encode method and how it can be used to convert the string data into bytes format:
Example 5: Decoding/Conversion to a String
Decoding is a process that converts the byte data back into the string format. This is done when the machine data is to be read by a human. The following code explains the decoding method:
str = b"I love to code!".decode('UTF-8')
print("The converted string is:", str)
print("The type of the converted data is:",type(str))
In the above code,
- The b keyword is added before the string “I love to code” which represents that it is a bytes data. The decode method with the parameter value of UTF-8 is called on this bytes string. The result is stored in the variable str.
- The string data along with its type is printed.
Output
The following output displays the decoding method that converts the byte string into a simple string consisting of a sequence of characters:
Conclusion
The b keyword before the string literal changes it into a byte literal that represents a sequence of bytes also known as the encoded data. The passing of information in the machine world requires binary data. Using a byte string is an efficient way of performing the encoding process. The article discusses the b keyword’s functionality in detail and also discusses its relevant examples.