Saturday, December 28, 2024
Google search engine
HomeLanguagesEffect of ‘b’ character in front of a string literal in Python

Effect of ‘b’ character in front of a string literal in Python

In Python, the ‘b‘ character before a string is used to specify the string as a “byte string“.By adding the ‘b’ character before a string literal, it becomes a bytes literal. This means that the string content should be interpreted as a sequence of bytes rather than characters. In this article, we will see the impact of the ‘b’ character preceding a string literal in Python.

What does the ‘b’ character do in front of a string literal?

When using Python, adding the ‘b’ character before a string literal creates a bytes literal. This means that the string content should be considered as a series of bytes and not characters. Bytes literals are utilized for representing binary data like encoded text, pictures, audio, or any other type of data that may not directly correspond to characters.

Example: The ‘b’ character, when placed in front of a string literal, signifies that the literal is to be treated as a bytes literal.

Python3




binary_data = b'Lazyroar'
print(binary_data)


Output

b'Lazyroar'

Difference between Strings and Byte Strings

Strings are normal characters that are in human-readable format whereas Byte strings are strings that are in bytes. Generally, strings are converted to bytes first just like any other object because a computer can store data only in bytes. When working with byte strings, they are not converted into bytes as they are already in bytes.

Example: Here is the code implementation of showing the difference between string and byte string in Python.

Python3




# String Example
text_string = "GeeksForGeeks"
print("Original String:", text_string)
print("Type of String:", type(text_string))
 
# Byte String Example
byte_string = b"GeeksForGeeks"
print("Original Byte String:", byte_string)
print("Type of Byte String:", type(byte_string))


Output

Original String: GeeksForGeeks
Type of String: <class 'str'>
Original Byte String: b'GeeksForGeeks'
Type of Byte String: <class 'bytes'>

How are Strings Converted to Bytes?

Strings are converted to bytes, using encoding. There are various encoding formats through which strings can be converted to bytes. For eg. ASCII, UTF-8, etc…

To convert a string to a byte string in Python:

Python3




var = 'Hey I am a String'.encode('ASCII')
print(var)


Output

b'Hey I am a String'

If we even print the type of the variable, we will get the byte type:

Python3




var = 'Hey I am a String'.encode('ASCII')
print(type(var))


Output

<class 'bytes'>

How is a Byte Object Converted to a String?

Just like encoding is used to convert a string to a byte, we use decoding to convert a byte to a string:

Python3




var = b'Hey I am a Byte String'.decode('ASCII')
print(var)


Output

Hey I am a Byte String

If we even print the type of variable, we will get the string type:

Python3




var = b'Hey I am a String'.decode('ASCII')
print(type(var))


Output

<class 'str'>

RELATED ARTICLES

Most Popular

Recent Comments