What does the 'b' character do in front of a string literal?

前端 未结 9 931
醉梦人生
醉梦人生 2020-11-21 05:07

Apparently, the following is the valid syntax:

my_string = b\'The string\'

I would like to know:

  1. What does this b
9条回答
  •  無奈伤痛
    2020-11-21 05:37

    The b denotes a byte string.

    Bytes are the actual data. Strings are an abstraction.

    If you had multi-character string object and you took a single character, it would be a string, and it might be more than 1 byte in size depending on encoding.

    If took 1 byte with a byte string, you'd get a single 8-bit value from 0-255 and it might not represent a complete character if those characters due to encoding were > 1 byte.

    TBH I'd use strings unless I had some specific low level reason to use bytes.

提交回复
热议问题