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:

    >>>a = b'string'
    >>>a

    'string'
    >>> type(a)

    <type 'str'>

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

    thanks,
    Stef Mientki
    Stef Mientki, Sep 1, 2010
    #1
    1. Advertising

  2. 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.

    Regards,
    Martin
    Martin v. Loewis, Sep 4, 2010
    #2
    1. Advertising

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


    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
    Martin v. Loewis, Sep 4, 2010
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nicolas Matringe

    Synthesizable (kind of) dual-edge FF

    Nicolas Matringe, Sep 27, 2004, in forum: VHDL
    Replies:
    1
    Views:
    929
    Ralf Hildebrandt
    Oct 12, 2004
  2. nb
    Replies:
    0
    Views:
    473
  3. Tom Anderson
    Replies:
    1
    Views:
    337
    Eric Sosman
    Nov 28, 2010
  4. Sam Kong
    Replies:
    5
    Views:
    93
    Andrew Johnson
    Mar 13, 2006
  5. Daniel Waite
    Replies:
    9
    Views:
    116
    Yossef Mendelssohn
    Sep 28, 2007
Loading...

Share This Page