Table of Contents
In this post, we will see how to convert dict to String in Python.
Python contains several data structures, and more often than not, there is a need for the conversion of one type to another. This tutorial focuses on and demonstrates the different ways available to convert a Dictionary object to a String in Python.
What is a dictionary in Python?
A dictionary, which is one of the four in-built data types that are provided in the Python’s arsenal to store data, that stores elements in the form of key:value
pairs. Dictionaries are an ordered collection.
A simple dictionary can be created as shown in the following example.
1 2 3 4 5 6 7 8 |
dictx = { "Company": "Samsung", "Device": "Galaxy", "Android version": 8 } print(dictx) |
What is a string in Python?
A string, as its name suggests, is utilized for representing Unicode characters. Usually marked around either single or double quotes, strings can hold any type of value.
A simple string literal can be created as shown in the following example.
1 2 3 |
print("Hello") # or print('Hello') |
How to convert dict to string in python?
- Using
str()
method. - Using pickle module.
- Using a
for
loop along. - Using
json.dumps()
.
Using str()
method.
Python’s built-in str()
method is utilized to convert the value passed as its argument into a string. Meanwhile, the literal_eval()
function from the ast
module is utilized to reverse or perform the opposite of this process.
The str()
module has a really basic syntax which can be found below.
1 2 3 |
z = str(19) |
The above code converts the int
value 19
into a string.
This str()
function can also be utilized to convert the contents of a dictionary into a string by applying the same rules and following the same steps as we normally do.
The following code uses the str()
function to convert a dictionary to a string in python.
1 2 3 4 5 6 7 8 9 10 11 12 |
dictx = { "Company": "Samsung", "Device": "Galaxy", "Android version": 8 } print(f"Given dictionary: {dictx}") print(type(dictx)) strx = str(dictx) # Convert dictionary to string print(f"Converted string: {strx}") print(type(strx)) |
The above code provides the following output:
Converted string: {‘Company’: ‘Samsung’, ‘Device’: ‘Galaxy’, ‘Android version’: 8} #Shows class ‘str’
Using the pickle
module.
The pickle
module needs to be installed and imported in Python and is capable of implementing binary protocols that serialize and de-serialize the structure of an object in Python. To put it simply, the pickle
module is utilized in converting any given Python object into a stream of characters.
The pickle.dumps()
function from this module can be utilized to implement the given task of converting a dictionary to a string in python. Meanwhile, the pickle.loads()
module is utilized to carry out the reverse of this process.
The following code uses the pickle
module to convert a dictionary to a string in python.
1 2 3 4 5 6 7 8 9 10 |
import pickle dictx = { "Company": "Samsung", "Device": "Galaxy", "Android version": 8 } x = pickle.dumps(dictx) print(x) |
The above code provides the following output:
Using the for
loop.
Instead of using any other modules or functions, we can simply implement this task with the help of a basic for
loop. However, we need several other functions with the iteration statement like the join()
function and the items()
function.
The following code uses the for
loop to convert a dictionary to a string in python.
1 2 3 4 5 6 7 8 |
dict = {'Company': " Samsung", 'Device': " Galaxy", 'Android version': " 8"} print(f"Given dictionary: {dict}") print(type(dict)) str = ', '.join(key + value for key, value in dict.items()) print(f"Converted string: {str}") print(type(str)) |
The above code provides the following output:
Converted string: Company Samsung, Device Galaxy, Android version 8
Further reading:
Using the json.dumps()
method.
JSON
is an abbreviation for JavaScript Object Notation
and is mainly just text written in a JavaScript format. It is utilized as a syntax for depositing data and also as a medium for the exchange of data. The in-built JSON
module in Python helps in working with the data that is stored as JSON
files.
The json.dumps
method is utilized to convert a Python dictionary into a string while the json.loads()
method operates as a complete opposite to the former.
The following code uses the json.dumps()
method to convert a dictionary to a string in python.
1 2 3 4 5 6 |
import json dictx = {'Company': "Samsung", 'Device': "Galaxy", 'Android version': 8} x = json.dumps(dictx) print(x) |
The above code provides the following output:
That’s all about how to dict to String in Python.