Home > Software engineering >  How i can use \n (new character) to insert a blank line between each key-value pair in dictionary i
How i can use \n (new character) to insert a blank line between each key-value pair in dictionary i

Time:09-16

glossary = { 'docstring': 'it is a short form of documentation string.', 'title': 'it is form of just heading.'}

Print('glossary')

CodePudding user response:

Use the pprint library as @don't talk just code said.

It makes list, dictionary more readable

import pprint
glossary = { 'docstring': 'it is a short form of documentation string.', 'title': 'it is form of just heading.'}

pprint.pprint(glossary)

Output

{'docstring': 'it is a short form of documentation string.',
 'title': 'it is form of just heading.'}

You could use str.replace if you do not want to use a library.

glossary = { 'docstring': 'it is a short form of documentation string.', 'title': 'it is form of just heading.'}

print( str(glossary).replace(",", ",\n") )

This makes the dictionary a string, and replace every comma , to a comma with a new line ,\n.

This probably has a lot of limitation

  • Related