How to Convert Dictionary to String in Python

In Python, there are multiple data structures that are used to store the value of input data. Dictionary is a popular data type that stores objects in an unordered set. It stores the collection of items in the key-value pair format. While dealing with various data types, the inter-conversion between datatypes is essential to serve the specific functionality.

In this post, we will demonstrate numerous methods to convert Dictionary value to String value in Python.

So, let’s get started!

Method 1: Using the str() Function

In Python, the built-in “str()” function is used to convert the Dictionary value into the String. The “str()” function is not recommended for large values, but it is an efficient method to convert the value into a string. Any data type value, including float, integer, or Dictionary, will be converted to string using the str() function.

how to handle linux files and direc...

Let’s see an example of “str()” function:

Code

#using str() function to convert dict to string
dict_value = {
  "School": "Capital",
  "Location": "USA",
  "Total Students": 100
}
print(f"Given dictionary: {dict_value}")
print(type(dict_value))
str_value = str(dict_value) # Convert dictionary to string
print(f"Converted string: {str_value}")
print(type(str_value))

In the above code:

  • Three dictionary keys with their values are stored in a variable named “dict_value”.
  • The “f-string” method prints the dictionary variable inside the string.
  • To confirm the dictionary data type, the “type()” function is applied to the variable “dict_value”.
  • To convert Dictionary into the string value, the “str()” function is used and passes the value of a variable inside the “str()” function.
  • The “type()” function is again used after conversion to check the type of variable named “str_value”.

Output

In the above output, we have converted the dictionary value into the string value.

Method 2: Using the json.dumps() Function

In Python, a “JSON” module named “json.dumps()” is used to convert dictionary value to string. The “json.dumps()” function easily parse the strings of any Python object. The “json.dumps()” function takes a dictionary object into its parameter and returns the value in strings.

Let’s understand the working of the “json.dumps()” via the example of code:

Code

#using json.dumps
import json
dict_value = {'School': "Capital", 'Location': "USA", 'Total No Of Students': 100}
print(dict_value)
print(type(dict_value))
string_value = json.dumps(dict_value)
print(string_value)
print(type(string_value))

In the above output:

  • Firstly, the “JSON” library is imported into the program.
  • Secondly, the value will be initialized with the dictionary data type.
  • We print the original Dictionary and the “type()” function.
  • The “json.dumps()” function converts the dictionary into strings.
  • The final string value will be printed along with the data type.

Output

The above output shows the conversion of the dictionary values into string values.

Method 3: Using the For Loop

For loop is also used to convert a dictionary into a string with the help of some inbuilt functions like “.join()” and “.items()”. The “join()” function joins all the dictionary elements using the for loop iteration and returns the value in a string. The “item()” function calls each key value in the dictionary.

Let’s understand their working via the following example:

#using For Loop() function
dict_value = {'School': " Capital", 'Location': " USA", 'Total Students': " 100"}
print(f"Given dictionary: {dict_value}")
print(type(dict_value))
str_value = ', '.join(key + value for key, value in dict_value.items())
print(f"Converted string: {str_value}")
print(type(str_value))

In the above code:

  • A dictionary variable is initialized named “dict_value
  • The original dictionary value was printed using the “f-string”.
  • The for loop is used along with the “join()”function to access the keys by iterating the dictionary items.
  • The converted string will be printed using the “f-string” function.

Output

The output shows that the dictionary items are converted into string values. Lastly, their type is also printed.

Method 4: Using the pickle.dumps() Function

Pickle is another single-purpose library or module which uses the “pickle.dumps()” function to convert a dictionary into a string. Pickle is less popular than the “json” Module because “json” is supported across many other platforms.

The following code example uses the Pickle module to convert a dictionary into a string:

Code

import pickle
dict_value = {
  "School": "Capital",
  "Location": "USA",
  "Total No Of Students": 100
}
string_value = pickle.dumps(dict_value)
print(string_value)

dict_value1 = pickle.loads(string_value)
print(dict_value1)
print(type(dict_value1))

In the above code:

  • The pickle library is imported.
  • The dictionary variable named “dict_value” is initialized.
  • Using the “pickle.dumps()” function, we convert the dictionary into the string.
  • The string value will be printed and stored in the variable named “string_value
  • Lastly, the “pickle.loads()” function is utilized to convert the string back into the dictionary and save the value in the variable named “dict_value1”. (this is an optional step here)

Output

The output shows that we have converted the dictionary into a string and from the string back into the dictionary.

That’s all from this Python guide!

Conclusion

Python provides multiple ways to convert a dictionary to a string in Python. The “str()”, “json.dumps()”, and the “pickle.dumps()” functions are the key player in converting a dictionary value to a string in Python. All methods successfully convert the dictionary value into a string using different approaches. This post has demonstrated all these Python methods to convert a dictionary variable value to a string.