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
- Import json library.
- Given a list in x.
- Call json.dumps() method and pass x as argument to the method.
json.dumps(x)
- 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)
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
- Import json library.
- Given a list in x.
- Convert each element in the list to string using list comprehension and str() built-in function.
x = [str(element) for element in x]
- Call json.dumps() method and pass x as argument to the method.
json.dumps(x)
- 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)
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.