views:

162

answers:

2

In a python source code I stumbled upon I've seen a small b before a string like in:

b"abcdef"

I know of u prefix that means unicode and r prefix that means raw.

What does the b stand for and in which kind of source code is it useful as it seems to be exactly like a plain string without any prefix ?

A: 

The b stands for binary, see info about Python 3.

jhwist
`b'Hi'` is a *byte* string literal. `0b0100101` is a *binary* literal
JimB
+6  A: 

This is Python3 bytes literal. This prefix is absent in Python 2.5 and older (it is equivalent to a plain string of 2.x, while plain string of 3.x is equivalent to a literal with u prefix in 2.x). In Python 2.6+ it is equivalent to a plain string, for compatibility with 3.x.

wRAR
@WRAR: Was in python 2.6 code I saw this, seems it was introduced in python2.6
kriss
I specifically checked it in the 2.6 reference before posting: http://docs.python.org/reference/lexical_analysis.html#literals
wRAR
OK, "For future compatibility, Python 2.6 adds bytes as a synonym for the str type, and it also supports the b'' notation.", from the "What's new".
wRAR