'why len(x) is zero when x=b''?

I am new to python. Can anybody please explain how len() works in python3 for the below mentioned example?

x = b''
print(len(x))

The output comes out to be 0.

A SyntaxError: invalid syntax is displayed if any value other than b'' is used. (eg: a'', c'', bb'', b!)



Solution 1:[1]

the b prefix indicate that that string is a byte string. Therefore, the x is actually a sequence of bytes with a length of 0.

>>> len('')
0    
>>> len(b'')
0

Edit: as @Marco Bonelli has pointed out, b'' is a bytes object, which is not same as '' (a str object)

>>> ('' == '')
True
>>> (b'' == '')
False

Solution 2:[2]

First, I think the other answer from yesterday is correct. b'' is a bytes object, but bb'' or c'' or a'' is not any type of obj. :))).

But you can use some letter about that (example r(raw string)). Ex:

example_var = r"hi \n hello"
print(example_var)
#Do you think it's print hi
#hello? It's incorrect :>. I tried to do that in my IDE before. It's print hi \n hello

Thanks for very much reading.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 ouflak