How to convert dictionary values to strings in Python

2 Answers

0 votes
dictionary = {'red': 1, 'green': 2, 'blue': 3}

dictionary_str = {k: str(v) for k, v in dictionary.items()}

print(dictionary_str)



'''
run:

{'red': '1', 'green': '2', 'blue': '3'}

'''

 



answered Feb 27 by avibootz
0 votes
dictionary = {'red': 1, 'green': 2, 'blue': 3}

for k in dictionary:
    dictionary[k] = str(dictionary[k])

print(dictionary)



'''
run:

{'red': '1', 'green': '2', 'blue': '3'}

'''

 



answered Feb 27 by avibootz
...