Home > Net >  Convert python dictionary to one string
Convert python dictionary to one string

Time:03-31

I am trying to convert a python dictionary to one string Example:

{'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}

should be

key1=value1; key2=value2; key3=value3

I tried to search for such a topic but didn't find any clue

This is my try but I am seeking to learn

tmp = ''
for cookie in cookies_dic:
    tmp = tmp   ';'   cookie   '='   cookies_dic[cookie]

print(tmp[1:])

CodePudding user response:

use this code

t = []
for i in d:
   t.append(f"{i} : {d[i]}")  
s = " ;".join(t)
print(s)

CodePudding user response:

d={'key1': 'value1', 'key2': 'value2', 'key3': 'value3'}
result=''
for i,j in d.items():
     result =i '=' j ';'
print(result)

key1=value1;key2=value2;key3=value3;
  • Related