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

Discussion in 'Python' started by Stef Mientki, Sep 1, 2010.

  1. Stef Mientki

    Stef Mientki Guest

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

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

    Stef Mientki
    Stef Mientki, Sep 1, 2010
    1. Advertisements

  2. Am 01.09.2010 23:32, schrieb Stef Mientki:
    It's redundant.

    Martin v. Loewis, Sep 4, 2010
    1. Advertisements

  3. 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.)

    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 Behnel, Sep 4, 2010
  4. Am 04.09.2010 19:27, schrieb Stefan Behnel:
    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


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

    Martin v. Loewis, Sep 4, 2010
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.