In this python tutorial, you will learn what ‘b’ will do in front of a string.
Table of Contents
Define “b”
In Python, the “b” stands for bytes, and it is used to refer to a byte string. The type of a byte string is “bytes”. Where as the type of normal string is “str”. A Byte string contains hexadecimal elements, and each hexadecimal value is between 0 and 255.
Example:
b'Welcome to thisPointer'
How to create a byte string?
It is possible to create a byte string using the following syntax.
Syntax:
b'input_str'
Where input_str is the input string.
Frequently Asked:
Example:
In this example, we will create a byte string.
# Consider the input byte string input_str = b'welcome to thisPointer' # Display the string print("Byte String: ",input_str) print('Type: ' , type(input_str))
Output:
Byte String: b'welcome to thisPointer' Type: <class 'bytes'>
Encode string into a byte
It’s time to convert an actual string into a byte string using encode() function. This function encodes the string into a byte string.
Syntax:
input_str.encode()
Where input_str is the input string.
Example 1:
In this example, we will convert the string – ‘welcome to thisPointer’ to a byte string.
# Consider the input string input_str ='welcome to thisPointer' # Display the string print("Actual String: ",input_str) print('Type: ' , type(input_str)) # Convert into byte string byte_str = input_str.encode() print("Converted String: ", byte_str) print('Type: ' , type(byte_str))
Output:
Actual String: welcome to thisPointer Type: <class 'str'> Converted String: b'welcome to thisPointer' Type: <class 'bytes'>
The string – “welcome to thisPointer” is converted into byte string.
Encode a string to byte string with UTF-8 encoding.
Here, we will take a string and encode it to a byte string with UTF-8 encoding, using the encode() method.
Syntax:
inp_str.encode('UTF-8')
Where inp_str is the Unicode string.
Example:
In this example, we will convert the string – “Welcome to thisPointer” to UTF-8.
# Consider the string inp_str= "Welcome to thisPointer" # Convert string to UTF-8 encoding inp_str=inp_str.encode('UTF-8') print("Converted String: ", inp_str) print('Type: ' , type(inp_str))
Output:
Converted String: b'Welcome to thisPointer' Type: <class 'bytes'>
We converted the above string to a byte string with UTF-8 encoding. It takes 1 byte for each character in the input string.
Encode a string to byte string with UTF-16 encoding.
Here, we will take a string and encode it to UTF-16 using encode() method.
Syntax:
inp_str.encode('UTF-16')
Where inp_str is the Unicode string.
Example:
In this example, we will convert the string – “Welcome to thisPointer” to UTF-16 byte string.
inp_str= "Welcome to thisPointer" # Convert string to UTF-16 encoding inp_str=inp_str.encode('UTF-16') print("Converted String: ", inp_str)
Output:
Converted String: b'\xff\xfeW\x00e\x00l\x00c\x00o\x00m\x00e\x00 \x00t\x00o\x00 \x00t\x00h\x00i\x00s\x00P\x00o\x00i\x00n\x00t\x00e\x00r\x00'
We converted the above string to a byte string with UTF-16 encoding. It takes 2 bytes for each character in the input string.
Encode a string to byte string with UTF-32 encoding.
Here, we will take a string and encode it to UTF-32 using encode() method.
Syntax:
inp_str.encode('UTF-32')
Where inp_str is the Unicode string.
Example:
In this example, we will convert the string – “Welcome to thisPointer” to UTF-32.
# Consider the string inp_str= "Welcome to thisPointer" # Convert string to UTF-32 encoding inp_str=inp_str.encode('UTF-32') print("Converted String: ", inp_str)
Output:
Converted String: b'\xff\xfe\x00\x00W\x00\x00\x00e\x00\x00\x00l\x00\x00\x00c\x00\x00\x00o\x00\x00\x00m\x00\x00\x00e\x00\x00\x00 \x00\x00\x00t\x00\x00\x00o\x00\x00\x00 \x00\x00\x00t\x00\x00\x00h\x00\x00\x00i\x00\x00\x00s\x00\x00\x00P\x00\x00\x00o\x00\x00\x00i\x00\x00\x00n\x00\x00\x00t\x00\x00\x00e\x00\x00\x00r\x00\x00\x00'
We converted the above string to a byte string with UTF-32 encoding. It takes 4 bytes for each character in the input string.
Summary
In this Python string tutorial, we have seen how to create a byte string and convert the normal string into a byte string using encode() function.