एक स्ट्रिंग बनाने के लिए आपको बाइट्स ऑब्जेक्ट को डीकोड करना होगा। यह स्ट्रिंग क्लास से डिकोड फ़ंक्शन का उपयोग करके किया जा सकता है जो उस एन्कोडिंग को स्वीकार करेगा जिसे आप डीकोड करना चाहते हैं।
उदाहरण
my_str = b"Hello" # b means its a byte string
new_str = my_str.decode('utf-8') # Decode using the utf-8 encoding
print(new_str) आउटपुट
यह आउटपुट देगा
Hello
एक बार जब आपके पास एक स्ट्रिंग के रूप में बाइट्स हों, तो आप स्ट्रिंग ऑब्जेक्ट को JSON में बदलने के लिए JSON.dumps विधि का उपयोग कर सकते हैं।
उदाहरण
my_str = b'{"foo": 42}' # b means its a byte string
new_str = my_str.decode('utf-8') # Decode using the utf-8 encoding
import json
d = json.dumps(my_str)
print(d) आउटपुट
यह आउटपुट देगा -
"{\"foo\": 42}"