Python – Print list with double quotes

Python – Print list with double quotes

When you print a list to console output in Python, the string elements in the list are printed with single quotes around them. But we need double quotes to be printed around them.

To print list with double quotes in Python, you can use json library. Call json.dumps() method and pass the given list as argument. dumps() method returns a JSON formatted version of the given list where the string elements are surrounded in double quotes.

For example, consider the following list.

x = ['apple', 'banana', 'cherry']

When you print this list to output using print() built-in function, you would get the following output.

['apple', 'banana', 'cherry']

But, our requirement is that the string elements must be surrounded in double quotes as shown below.

["apple", "banana", "cherry"]

1. Print list with double quotes for string elements using json.dumps() method in Python

Consider that we are given a list in x, and we have to print this list with double quotes using json library.

Steps

  1. Import json library.
  2. Given a list in x.
  3. Call json.dumps() method and pass x as argument to the method.
json.dumps(x)
  1. The method returns a JSON array representation of the list, where the string elements in the list are surrounded with double quotes because of the JSON standard of representing a string in double quotes. Store the returned string in a variable, say x_double_quotes, and print it to standard console output using print() built-in function.

Program

The complete program to print a list of string elements with double quotes using json.dumps() method.

Python Program

import json

# Given list
x = ['apple', 'banana', 'cherry']

# Convert list to JSON string
x_double_quotes = json.dumps(x)

# Print the list with double quotes
print(x_double_quotes)
Run Code Copy

Output

["apple", "banana", "cherry"]

The elements in list are printed with double quotes.

What if you want all the elements in the list, irrespective of data type, whether the list element is a string or not.

2. Print list with double quotes for all elements in Python

Consider that we are given a list in x, and we have to print this list with double quotes for all elements in the list.

Steps

  1. Import json library.
  2. Given a list in x.
  3. Convert each element in the list to string using list comprehension and str() built-in function.
x = [str(element) for element in x]
  1. Call json.dumps() method and pass x as argument to the method.
json.dumps(x)
  1. The method returns a JSON array representation of the list, where the string elements in the list are surrounded with double quotes. Print the returned string to standard console output using print() built-in function.

Program

The complete program to print a list of elements, with double quotes around each element.

Python Program

import json

# Given list
x = ['apple', 'banana', 100, 200]

# Convert each element to string
x = [str(element) for element in x]

# Convert list to JSON string
x_double_quotes = json.dumps(x)

# Print the list with double quotes
print(x_double_quotes)
Run Code Copy

Output

["apple", "banana", "100", "200"]

Summary

In this tutorial, we have seen how to print a given list, with double quotes around elements of the list, to standard output, using json dumps() function, with well detailed examples.

Related Tutorials

Code copied to clipboard successfully 👍