Reputation: 1410
I am having a dictionary :-
test_list = {"fname":"eoin","dob":"10/12/1992"}
As this is just a sample dictionary but in my case the items of dictionary is dynamic it could be of any length. So I expect to create a string in this format
result = "fname : {fname} , dob: {dob}".format(**test_list)
As I would need this for my other purpose. I don't know how to do this.
Upvotes: 3
Views: 81
Reputation: 92854
Simple dict's string representation:
test_dict = {"fname":"eoin","dob":"10/12/1992","abc": 123}
res = ', '.join('{}: {}'.format(k, v) for k,v in test_dict.items())
print(res)
The output:
fname: eoin, dob: 10/12/1992, abc: 123
Upvotes: 2
Reputation: 82755
One approach is to use str()
with str.strip
Ex:
test_dict = {"fname":"eoin","dob":"10/12/1992"}
print(str(test_dict).strip("{}").replace("'", "").replace('"', ""))
Output:
fname: eoin, dob: 10/12/1992
Upvotes: 1
Reputation: 71560
Try using this code:
print(str(test_list)[1:-1].replace("'", ""))
Output:
fname: eoin, dob: 10/12/1992
Upvotes: 2