B in Front of String in Python

This tutorial will discuss the b"
statement in Python.
Use the b"
Statement in Python
The b"
notation is used to specify a bytes
string in Python. Compared to the regular strings, which have ASCII characters, the bytes
string is an array of byte variables where each hexadecimal element has a value between 0 and 255.
We can also encode regular strings into bytes
strings with the built-in encode()
function. The program below shows us how to encode regular strings into bytes
strings with the encode()
function.
string = 'This is a string'
print(string.encode())
Output:
b'This is a string'
We encoded the regular string This is a string
into a bytes
string format with the encode()
function in the code above. We can also encode a string into a bytes
string format with the b"
statement. The following code snippet shows us how we can do that.
string = b'This is a string'
print(string)
Output:
b'This is a string'
Here, the string
variable is not a regular string; instead, it is a bytes
string.
Maisam is a highly skilled and motivated Data Scientist. He has over 4 years of experience with Python programming language. He loves solving complex problems and sharing his results on the internet.
LinkedInRelated Article - Python Bytes
- Convert Bytes to Int in Python 2.7 and 3.x
- Convert Int to Bytes in Python 2 and Python 3
- Convert Int to Binary in Python
- Convert Bytes to String in Python 2 and Python 3
- Convert String to Bytes in Python