Explore Courses Blog Tutorials Interview Questions
+3 votes
in Python by (2.6k points)
edited by

Can someone tell me how to convert the bytes value back to string?

I have used binascii.b2a_qp() method, communicate() method and standard output from an external program but everything returns the same array of bytes, I want to do it with a normal python string and print this:

>>> print(command_stdout)
-rw-rw-r-- 1 AF AF 0 Jun  1 17:23 file1
-rw-rw-r-- 1 AF AF 0 Jun  1 17:23 file2

 Can anyone answer this, I want to use a "short cut" instead of doing it manually.

2 Answers

0 votes
by (46k points)
edited by

You just need to follow up with this easy syntax to decode the bytes to produce a string:

>>> w"qwerty"

# utf-8 is used here because it is a very common encoding, but you
# need to use the encoding your data is actually in.
>>> w"qwerty".decode("utf-8")

Enjoy Learning, Cheers....!!

0 votes
by (106k points)

You can use the following code:-

bytes_data = [112, 52, 52]

"".join(map(chr, bytes_data))

>> p44

You can use the following video tutorials to clear all your doubts:-

Related questions

0 votes
1 answer
asked Oct 11, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
+1 vote
2 answers
+3 votes
2 answers
+3 votes
2 answers
Welcome to Intellipaat Community. Get your technical queries answered by top developers!

28.4k questions

29.7k answers


94k users

Browse Categories