Open In App

How to Add New Line in Dictionary in Python

Last Updated : 25 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Dictionaries are key-value stores that do not inherently support formatting like new lines within their structure. However, when dealing with strings as dictionary values or when outputting the dictionary in a specific way, we can introduce new lines effectively. Let's explore various methods to add new lines in a dictionary in Python.

Using \n

We can use the \n escape sequence to add a new line within a string value in the dictionary.

Python
# Initialize a dictionary with a newline in the value
a = {"greeting": "Hello,\nWorld!"}

# Print the dictionary value
print(a["greeting"])

Output
Hello,
World!

Explanation:

  • The \n escape sequence is used to introduce a new line in the string value.
  • When the value is printed, the new line appears in the output.

Let's explore some more ways and see how we can add new line in Dictionary in Python.

Using for loop

We can dynamically add new lines to dictionary values by using a for loop while iterating.

Python
# Initialize a dictionary
a = {"line1": "This is line 1.", "line2": "This is line 2."}

# Add new lines dynamically
for key in a:
    a[key] += "\nNew Line"

# Print updated dictionary values
for value in a.values():
    print(value)

Output
This is line 1.
New Line
This is line 2.
New Line

Explanation:

  • During iteration, the += operator appends a new line to each value using \n.
  • This method is useful for applying consistent formatting across all dictionary values.

Formatting Output with json.dumps()

When displaying a dictionary, we can format it to include new lines using json.dumps() with the indent parameter.

Python
import json

# Initialize a dictionary
a = {"name": "Mazey", "age": 25, "city": "New York"}

# Format and print dictionary with new lines
print(json.dumps(a, indent=4))

Output
{
    "name": "Mazey",
    "age": 25,
    "city": "New York"
}

Explanation:

  • The json.dumps() method formats the dictionary for display, adding new lines between key-value pairs.
  • The indent parameter specifies the number of spaces for indentation.
  • This method is ideal for printing dictionaries in a structured and readable way.

Using Multiline String Values

We can directly use triple quotes (""" or ''') to define multiline string values in the dictionary.

Python
# Initialize a dictionary with multiline values
a = {
    "address": """123 Main St
Cityville
Countryland"""
}

# Print the dictionary value
print(a["address"])

Output
123 Main St
Cityville
Countryland

Explanation:

  • Multiline strings are defined using triple quotes, making it easy to include new lines in the value.
  • This approach is especially useful for storing structured text data like addresses or paragraphs.

Concatenating Strings with \n

We can construct strings with new lines programmatically by concatenating them using \n.

Python
# Initialize an empty dictionary
a = {}

# Add a value with concatenated new lines
a["description"] = "Line 1" + "\n" + "Line 2" + "\n" + "Line 3"

# Print the dictionary value
print(a["description"])

Output
Line 1
Line 2
Line 3

Explanation:

  • Strings are concatenated with +, using \n to insert new lines between parts.
  • This method allows dynamic construction of multiline strings.

Next Article
Practice Tags :

Similar Reads