435

For example, I have a string like this(return value of subprocess.check_output):

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

Whatever I did to it, it is always printed with the annoying b' before the string:

>>> print(b'a string')
b'a string'
>>> print(str(b'a string'))
b'a string'

Does anyone have any ideas about how to use it as a normal string or convert it into a normal string?

1

3 Answers 3

621

Decode it.

>>> b'a string'.decode('ascii')
'a string'

To get bytes from string, encode it.

>>> 'a string'.encode('ascii')
b'a string'
7
  • 39
    @lyomi, I used ascii because the given string was made with ascii letters. You don't need to specify encoding if the encoding is utf-8 (default in Python 3.x according to str.encode, bytes.decode doc-string)
    – falsetru
    Commented Mar 30, 2016 at 8:28
  • 2
    @lyomi In 2016 (and its nearly the end) people still use ascii. There are many many 'legacy' products and systems (including specifications), but there are also lots of reasons why you might be creating a 'binary string' where you don't want unicode or something to try and 'merge' multiple bytes into a single character. We often use 'strings' to contain binary data for instance making DNS requests etc.
    – Jmons
    Commented Sep 23, 2016 at 11:55
  • I suggest to add the following to complete the answer. Most times we need to decode bytes from our operating system, such as console output, the most pythonic way I found to do it is to import locale and then os_encoding = locale.getpreferredencoding(). This way, we can decode using my_b_string.decode(os_encoding)
    – aturegano
    Commented Jul 27, 2017 at 15:10
  • 2
    @aturegano, It's not the only option. sys.getfilesystemencoding(), sys.stdin.encoding, sys.stdout.encoding. IMHO, using those automatic encoding detection could solve problem because the sub-program (OP is using subprocess) could be written other way to determine encoding (or even hard-coded). Thanks for feedback, anyway.
    – falsetru
    Commented Jul 28, 2017 at 9:02
  • @falsetru Note that sys.getfilesystemencoding() returns the name of the encoding used to convert between Unicode filenames and bytes filenames and is strongly dependant on operating system you are using. AFAIK, this function is used to convert to the system’s preferred representation. That means that it will not infer the codification used by the console that can be obtained using the aforementioned locale.getpreferredencoding() function
    – aturegano
    Commented Jul 28, 2017 at 11:42
130

If the answer from falsetru didn't work you could also try:

>>> b'a string'.decode('utf-8')
'a string'
13

See the official encode() and decode() documentation from codecs library. utf-8 is the default encoding for the functions, but there are severals standard encodings in Python 3, like latin_1 or utf_32.

1
  • This post is helpful in that it remedies error messages like this, "'utf-8' codec can't decode byte..."
    – Tom Renish
    Commented Aug 19 at 21:27

Not the answer you're looking for? Browse other questions tagged or ask your own question.