We are committed to provide service 24 X 7

Deals, Shopping, Training, Tools

What does ‘b’ mean in front of a string in Python?

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.

Advertisements

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.

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'xffxfeWx00ex00lx00cx00ox00mx00ex00 x00tx00ox00 x00tx00hx00ix00sx00Px00ox00ix00nx00tx00ex00rx00'

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'xffxfex00x00Wx00x00x00ex00x00x00lx00x00x00cx00x00x00ox00x00x00mx00x00x00ex00x00x00 x00x00x00tx00x00x00ox00x00x00 x00x00x00tx00x00x00hx00x00x00ix00x00x00sx00x00x00Px00x00x00ox00x00x00ix00x00x00nx00x00x00tx00x00x00ex00x00x00rx00x00x00'

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.

Cart

Your Cart is Empty

Back To Shop