what is this kind of string: b'string' ?
Stefan Behnel
stefan_ml at behnel.de
Sat Sep 4 13:27:34 EDT 2010
Martin v. Loewis, 04.09.2010 18:52:
> Am 01.09.2010 23:32, schrieb Stef Mientki:
>> in winpdb I see strings like this:
>>
>> >>> a = b'string'
>> >>> a
>> 'string'
>> >>> type(a)
>> <type 'str'>
>>
>> what's the "b" doing in front of the string ?
>
> It's redundant.
Not completely. (I know that you know this, but to those who don't, your
answer may be misleading.)
If you use 2to3 to convert the above to Python 3 code, it will leave the
'b' in front of the string, so the resulting string literal will be a bytes
string in Python 3. If you remove it, the string will become a unicode
literal. Since the code is syntax compatible with Python 3, simply running
it in a Python 3 interpreter will also show this behaviour.
So it's redundant in Python 2, but it's no longer redundant when you plan
to migrate the code to Python 3.
Stefan
More information about the Python-list
mailing list