Change language

Python | Convert dictionary object to string

| |

Let’s see how you can change the dictionary to string.

Methods # 1: Using json.dumps()

json.dumps () — built-in function in json library. It takes precedence over pickle because it has cross-platform support.

# Python code for demo
# convert dictionary to string
# using json.dumps ()

 

import json

 
# initializing dictionary

test1 = { " testname " : "akshat" ,

"test2name" : "manjeet" < code class = "plain">,

"test3name" : "nikhil" }

  
# print original dictionary

print ( type (test1))

print ( "initial dictionary =" , test1 )

 
# convert dictionary to string
# using json.dumps ()

result = json.dumps (test1)

 
# oven output as a string

print ( "" , type (result))

print ( "final string =" , result)

Output:

"class ’dict’"
initial dictionary = {’testname’: ’akshat’, ’test2name’: ’manjeet’, ’test3name’: ’nikhil’}

"class ’str’"
final string = {“testname”: “akshat”, “test2name”: “manjeet”, “test3name”: “nikhil”}

Methods # 2: Using str()

The str () function converts the specified value to a line.

# Python code for demonstration
# convert dictionary to string
# using str ()

 
# initializing dictionary

test1 = { " testname " : "akshat" ,

"test2name" : "manjeet" ,

"test3name" : "nikhil" }

 
# print the original dictionary

print < / code> ( type (test1))

print ( "initial dictionary =" , test1)

  
# convert dictionary to string
# using page

result = str (test1)

  
# print the resulting string

print ( "" , type (result))

print ( "final string =" , result)

Output:

"class’ dict ’& gt;
initial dictionary = {’ test2name ’:’ manjeet ’,’ testname ’:’ akshat ’,’ test3name ’:’ nikhil ’}

"class’ str’"
final string = {’test2name’: ’manjeet’, ’testname’: ’akshat’, ’test3name’: ’nikhil’}

Python | Convert dictionary object to string __dict__: Questions

Python | Convert dictionary object to string Python functions: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method