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

S

Stef Mientki

in winpdb I see strings like this:
<type 'str'>

what's the "b" doing in front of the string ?

thanks,
Stef Mientki
 
M

Martin v. Loewis

Am 01.09.2010 23:32, schrieb Stef Mientki:
in winpdb I see strings like this:

<type 'str'>

what's the "b" doing in front of the string ?

It's redundant.

Regards,
Martin
 
S

Stefan Behnel

Martin v. Loewis, 04.09.2010 18:52:
Am 01.09.2010 23:32, schrieb Stef Mientki:

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
 
M

Martin v. Loewis

Am 04.09.2010 19:27, schrieb Stefan Behnel:
Martin v. Loewis, 04.09.2010 18:52:

Not completely. (I know that you know this, but to those who don't, your
answer may be misleading.)

Actually, I didn't think of this (knowing something and being aware of
it are different things ...)

I personally don't use it in the 2to3 way, because it requires Python
2.6. For code that needs to go back further, I typically do

b('string')

with a custom b() function. That's less efficient, of course, since it
causes a function call on evaluation.

Thanks,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top