Convert Dict to String in Python

Convert dict to String in Python

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.

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.

How to convert dict to string in python?

  1. Using str() method.
  2. Using pickle module.
  3. Using a for loop along.
  4. 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.

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.

The above code provides the following output:

Given dictionary: {‘Company’: ‘Samsung’, ‘Device’: ‘Galaxy’, ‘Android version’: 8} #Shows class ‘dict’
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.

The above code provides the following output:

b’\x80\x04\x95?\x00\x00\x00\x00\x00\x00\x00}\x94(\x8c\x07Company\x94\x8c\x07Samsung\x94\x8c\x06Device\x94\x8c\x06Galaxy\x94\x8c\x0fAndroid version\x94K\x08u.’

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.

The above code provides the following output:

Given dictionary: {‘Company’: ‘ Samsung’, ‘Device’: ‘ Galaxy’, ‘Android version’: ‘ 8’}

Converted string: Company Samsung, Device Galaxy, Android version 8

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.

The above code provides the following output:

{“Company”: “Samsung”, “Device”: “Galaxy”, “Android version”: 8}

That’s all about how to dict to String in Python.

Was this post helpful?

Leave a Reply

Your email address will not be published. Required fields are marked *