Given a dictionary having key:value pairs as byteString, the task is to convert the key:value pair to string.
Examples:
Input: {b'EmplId': b'12345', b'Name': b'Paras', b'Company': b'Cyware' }
Output: {'EmplId': '12345', 'Name': 'Paras', 'Company': 'Cyware'}
Input: {b'Key1': b'Geeks', b'Key2': b'For', b'Key3': b'Geek' }
Output: {'Key1':'Geeks', 'Key2':'For', 'Key3':'Geek' }
Method #1: By dictionary comprehension
# Python Code to convert ByteString key:value # pair of dictionary to String. # Initialising dictionary x = {b'EmplId': b'12345', b'Name': b'Paras', b'Company': b'Cyware'} # Converting x = { y.decode('ascii'): x.get(y).decode('ascii') for y in x.keys() } # printing converted dictionary print(x) |
chevron_right
filter_none
Output:
{'Name': 'Paras', 'EmplId': '12345', 'Company': 'Cyware'}
Method #2: By iterating key and values
# Python Code to convert ByteString key:value # pair of dictionary to String. # Initialising dictionary x = {b'EmplId': b'12345', b'Name': b'Paras', b'Company': b'Cyware'} # Initialising empty dictionary y = {} # Converting for key, value in x.items(): y[key.decode("utf-8")] = value.decode("utf-8") # printing converted dictionary print(y) |
chevron_right
filter_none
Output:
{'Company': 'Cyware', 'Name': 'Paras', 'EmplId': '12345'}
Recommended Posts:
- Python | Convert string dictionary to dictionary
- Python | Convert key-value pair comma separated string into dictionary
- Python | Convert flattened dictionary into nested dictionary
- Python | Convert nested dictionary into flattened dictionary
- Python - Convert Dictionary Value list to Dictionary List
- Python | Convert tuple to adjacent pair dictionary
- Python | Convert dictionary object into string
- Python - Convert Dictionary to Concatenated String
- Python - Convert key-value String to dictionary
- Python - Convert String List to Key-Value List dictionary
- Python | Pretty Print a dictionary with dictionary value
- Regular Dictionary vs Ordered Dictionary in Python
- Python | Dictionary initialization with common dictionary
- Python - Update dictionary with other dictionary
- Python - Filter dictionary values in heterogenous dictionary
- Python - Replace dictionary value from other dictionary
- Python - Append Dictionary Keys and Values ( In order ) in dictionary
- Add a key:value pair to dictionary in Python
- Python | Get random dictionary pair
- Python - Pair lists elements to Dictionary
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

