what is this kind of string: b'string' ?

Gary Herron gherron at islandtraining.com
Wed Sep 1 18:30:06 EDT 2010


On 09/01/2010 02:32 PM, Stef Mientki wrote:
>   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 ?
>
> thanks,
> Stef Mientki
>    

In Python2 the b is meaningless (but allowed for compatibility and 
future-proofing purposes), while in Python 3 it creates a byte array (or 
byte string or technically an object of type bytes) rather than a string 
(of unicode).

Python2
 >>> type(b'abc')
<type 'str'>
 >>> type('abc')
<type 'str'>

Python3:
 >>> type(b'abc')
<class 'bytes'>
 >>> type('abc')
<class 'str'>




More information about the Python-list mailing list