Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program To Convert dictionary values to Strings
A dictionary in Python is an unordered collection of key-value pairs. It is a data structure that allows you to store and retrieve values based on a unique key associated with each value. Keys in a dictionary must be immutable (strings, numbers, or tuples), while values can be of any data type and can be mutable.
When we want to convert dictionary values to strings, we need to iterate over the dictionary and convert each value to a string using the str() function.
Input Output Scenarios
See the following input-output scenarios to understand the concept of converting dictionary values to strings ?
# Input dictionary with integer values
input_dict = {'red': 3, 'yellow': 2, 'green': 3}
print("Input dictionary:", input_dict)
# Convert values to strings
output_dict = {key: str(value) for key, value in input_dict.items()}
print("Output converted dictionary:", output_dict)
Input dictionary: {'red': 3, 'yellow': 2, 'green': 3}
Output converted dictionary: {'red': '3', 'yellow': '2', 'green': '3'}
The output converted dictionary contains the original keys and their corresponding values converted to strings.
# Input dictionary with mixed data types
input_dict = {'k1': 12, 'numbers': [1, 2, 3], 'tuple of numbers': (10, 11, 2)}
print("Input dictionary:")
print(input_dict)
# Convert all values to strings
output_dict = {key: str(value) for key, value in input_dict.items()}
print("Output converted dictionary:")
print(output_dict)
Input dictionary:
{'k1': 12, 'numbers': [1, 2, 3], 'tuple of numbers': (10, 11, 2)}
Output converted dictionary:
{'k1': '12', 'numbers': '[1, 2, 3]', 'tuple of numbers': '(10, 11, 2)'}
In the converted dictionary, the integer 12, list [1, 2, 3], and tuple (10, 11, 2) are converted to strings '12', '[1, 2, 3]', and '(10, 11, 2)'.
Using a for Loop
In this approach, we update the original dictionary with converted string values. We iterate over the original dictionary using the items() method and convert each value to a string using the str() function ?
# Create the dictionary
dictionary = {'red': 3, 'yellow': 2, 'green': 3}
print('Input dictionary:', dictionary)
# Convert each value to string
for key, value in dictionary.items():
dictionary[key] = str(value)
# Display the output
print('Output converted dictionary:', dictionary)
Input dictionary: {'red': 3, 'yellow': 2, 'green': 3}
Output converted dictionary: {'red': '3', 'yellow': '2', 'green': '3'}
Using Dictionary Comprehension
This approach creates a new dictionary using dictionary comprehension. We iterate over the original dictionary using the items() method, and for each key-value pair, we convert the value to a string using the str() function ?
# Create the dictionary
dictionary = {'k1': 123, 'numbers': [1, 2, 3], 'tuple of numbers': (10, 11, 2)}
print('Input dictionary:')
print(dictionary)
# Create new dictionary with string values
converted_dict = {key: str(value) for key, value in dictionary.items()}
# Display the output
print('Output converted dictionary:')
print(converted_dict)
Input dictionary:
{'k1': 123, 'numbers': [1, 2, 3], 'tuple of numbers': (10, 11, 2)}
Output converted dictionary:
{'k1': '123', 'numbers': '[1, 2, 3]', 'tuple of numbers': '(10, 11, 2)'}
Using the join() Method
In this approach, we convert dictionary values into a single concatenated string. We use the join() method to concatenate all values together. The expression "".join() specifies an empty string as the separator between values ?
# Create the dictionary
dictionary = {1:'t', 2:'u', 3:'t', 4:'o', 5:'r', 6:'i', 7:'a', 8:'l', 9:'s', 10:'p', 11:'o', 12:'i', 13:'n', 14:'t'}
print('Input dictionary:')
print(dictionary)
# Join all values into a single string
converted_string = "".join(v for _, v in dictionary.items())
# Display the output
print('Output converted string:')
print(converted_string)
Input dictionary:
{1: 't', 2: 'u', 3: 't', 4: 'o', 5: 'r', 6: 'i', 7: 'a', 8: 'l', 9: 's', 10: 'p', 11: 'o', 12: 'i', 13: 'n', 14: 't'}
Output converted string:
tutorialspoint
Comparison
| Method | Creates New Dictionary? | Best For |
|---|---|---|
| For Loop | No (modifies original) | In-place modification |
| Dictionary Comprehension | Yes | Functional programming style |
| join() Method | No (creates string) | Concatenating values into single string |
Conclusion
Use dictionary comprehension for creating a new dictionary with string values, or use a for loop to modify the original dictionary in-place. The join() method is useful when you need to combine all values into a single string.
