How to Add New Line in Dictionary in Python Last Updated : 25 Jan, 2025 Summarize Comments Improve Suggest changes Share 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"]) OutputHello, 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.Table of ContentUsing for loopFormatting Output with json.dumps()Using Multiline String ValuesConcatenating Strings with \nUsing for loopWe 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) OutputThis 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 ValuesWe 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"]) Output123 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 \nWe 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"]) OutputLine 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. Comment More infoAdvertise with us Next Article How to Add New Line in Dictionary in Python A akritisip00h Follow Improve Article Tags : Python Python dictionary-programs Practice Tags : python Similar Reads Python Tutorial - Learn Python Programming Language Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly. It'sA high-level language, used in web development, data science, automation, AI and more.Known fo 10 min read Python Interview Questions and Answers Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth 15+ min read Non-linear Components In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co 11 min read Python OOPs Concepts Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p 11 min read Python Projects - Beginner to Advanced Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list 10 min read Python Exercise with Practice Questions and Solutions Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test 9 min read Python Programs Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co 11 min read Spring Boot Tutorial Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance 10 min read Python Introduction Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien 3 min read Python Data Types Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes 9 min read Like