The string is one of the most common data types in Python. A string is an array of characters. These characters can be anything that you can enter through your keyboard right now. Python strings are very flexible and have great functionalities despite being immutable. But have you ever seen a string with the character “b” before it? Ever wondered what does that mean?
In this article, we’re going to see what a b-string means and see how we can use it to our advantage.
What is a string?
A string is an array of characters. Each possible character in a string can be represented with an ASCII value, including the metacharacters like newline, tab, backspace, etc. Python gives a lot of importance to the strings. It even has a pool of string methods to work on a string, and if even that’s not enough, Python also has a string module as part of its standard library, which has even more functions that help in string manipulation.
Related: Learn more about strings in Python.
What is ASCII?
ASCII refers to the American standard code for information interchange. It is the most common way to encode Unicode characters. There’s only one ASCII value corresponding to each possible character. There are ASCII values even for space, tab, newline, or backspace. As programmers, we generally deal with characters in ASCII values. ASCII values are simply a decimal representation of the binary representation of the characters.
To get the ASCII value of a particular character, there’s a function called
ord(). There’s also a function called
chr() in Python, which is used to get the character from an ASCII value.
How to use the
ord() and the
Let’s see how we can find out the ASCII value of the letter “A” and the character corresponding to the ASCII value 65.
print("The ASCII value of the letter 'A' is:",ord('A')) print("The character correponding to ASCII value 65 is:",chr(65))
ord() method can get you an ASCII value of only one character at a time. What if you want the ASCII value of all the characters in a particular string? One way to get that is by using a loop.
So let’s say you have a string “Welcome to askPython”. Now you can loop through the entire string and get the ASCII value of each character.
string = "Welcome to askPython." for character in string: print(ord(character))
So this way, we can get the ASCII value of each character in a string. The ASCII values are simply byte-encoded characters. This way of converting each character to its byte-encoded format is tedious and time-consuming. Python also has another in-built method to convert a string to its byte-encoded format to deal with strings at low-level. Let’s see how we can do it.
b-string or b””
The b literal before a string converts the string into byte format. The advantage of converting it to byte format is that it can be represented in binary, and we can then perform low-level tasks on it.
How to create a b-string?
To create a byte string we just have to add a b literal before the string.
string = b"Welcome to askPython."
How do you know it’s not a normal string but a byte string?
We can check its datatype using the
string = "Welcome to askPython." print(type(string)) string = b"Welcome to askPython." print(type(string))
When you check the type of a string with a b literal before it, you can see it show the class as
bytes instead of a
But if you try to print it what do you you see?
string = b"Welcome to askPython." print(string)
It just prints the string with a b literal before it. Then what’s the point of doing it?
Let’s see what it shows when we try to access a character at a particular index in the string.
string = b"Welcome to askPython." print(string)
87 is the ASCII value of “W”. So when we try to print a particular character in a byte string, it prints the corresponding ASCII value. That means that index has the value as the ASCII value and not the actual character. This means that this is a byte string and not the actual string. So we can get the list of the ASCII values of all the characters by simply iterating through every byte and appending it to a list or we can simply use a list comprehension.
string = b"Welcome to askPython." byte_string = [byte for byte in string] print(byte_string)
In the above code, we simply created a byte string using the b literal and then used list comprehension to generate a list of all bytes.
Related: Learn how to use list comprehensions.
encode() is a method to convert a normal string to a byte string and
decode() is a method to convert a byte string back to a normal string. Both are in-built Python functions and have great computer network applications. They are used to encode and decode a text message to send and receive it on different devices around a computer network. Let’s see how can we use them.
string = "Welcome to askPython." ilist =  for character in string: ilist.append(character) print("Initial string:",ilist) string = string.encode() blist =  for character in string: blist.append(character) print("Encoded string:",blist) clist =  string = string.decode() for character in string: clist.append(character) print("Decoded string:",clist)
In the above block of code, first, we took the same string as before in normal string format. We stored each character of the initial string in the list ilist. Then we encoded the list using the
encode() method. Then we again stored each character in a different list called blist. We can see that this list is the same list as before. That means this is the byte-string like before. So now when we used the
decode() method on this byte-string, we got the same string as before.
Related: Learn the encode() and decode() functions in depth.
The byte string serves as the basis of message transmission today. We have many advanced encoding formats nowadays but the byte string is a beginner-friendly and very powerful way of text encoding. Python makes it very easy to work on strings using this byte-string conversion method. Make sure you get the basics clear else it will get really hard moving forward.
Stack Overflow answer for the same question.