Print a dictionary without brackets in Python

avatar

Borislav Hadzhiev

Last updated: Sep 15, 2022

banner

Photo from Unsplash

Print a dictionary without brackets in Python #

To print a dictionary without brackets:

  1. Format the items of the dictionary into strings.
  2. Use the str.join() method to join the strings into a single string.
  3. Use the print() function to print the string.
main.py
my_dict = { 'id': 1, 'name': 'bobbyhadz', 'salary': 100 } # ✅ print dictionary without brackets on multiple lines result = '\n'.join(f'{key}: {value}' for key, value in my_dict.items()) # id: 1 # name: bobbyhadz # salary: 100 print(result) # -------------------------------------- # ✅ print dictionary without brackets in a single line result = ', '.join(f'{key}: {value}' for key, value in my_dict.items()) print(result) # 👉️ id: 1, name: bobbyhadz, salary: 100

We used the str.join() method to print a dictionary without brackets.

We passed a generator expression to the str.join() method.

main.py
my_dict = { 'id': 1, 'name': 'bobbyhadz', 'salary': 100 } result = '\n'.join(f'{key}: {value}' for key, value in my_dict.items()) # id: 1 # name: bobbyhadz # salary: 100 print(result)
Generator expressions are used to perform some operation for every element or select a subset of elements that meet a condition.

On each iteration, we use a formatted string literal to format the key and value into a string.

Formatted string literals (f-strings) let us include expressions inside of a string by prefixing the string with f.

main.py
var1 = 'bobby' var2 = 'hadz' result = f'{var1}{var2}' print(result) # 👉️ bobbyhadz

Make sure to wrap expressions in curly braces - {expression}.

The str.join method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.

The string the method is called on is used as the separator between the elements.

We used a newline (\n) character as the separator in the first example, but you can use any other value, e.g. a comma and a space.

main.py
my_dict = { 'id': 1, 'name': 'bobbyhadz', 'salary': 100 } result = ', '.join(f'{key}: {value}' for key, value in my_dict.items()) print(result) # 👉️ id: 1, name: bobbyhadz, salary: 100

If you need to print the dictionary without any other value, e.g. without the square brackets of a list, use the str.replace() method.

main.py
my_dict = { 'id': 1, 'name': ['bobbyhadz'], 'salary': [100] } result = str(my_dict).strip('}{').replace('[', '').replace(']', '') print(result) # 👉️ 'id': 1, 'name': 'bobbyhadz', 'salary': 100
We converted the dictionary to a string and used the str.strip() method to remove the curly braces of the dictionary when printing it.

The str.strip method returns a copy of the string with the specified leading and trailing characters removed.

We used the str.replace() method to remove the square brackets of each list value in the dictionary.

The str.replace method returns a copy of the string with all occurrences of a substring replaced by the provided replacement.

The method takes the following parameters:

NameDescription
oldThe substring we want to replace in the string
newThe replacement for each occurrence of old
countOnly the first count occurrences are replaced (optional)

We used an empty string as the replacement because we want to remove all square brackets from the string.

You can chain as many calls to the str.replace() method as necessary.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.