Problem Formulation
π¬ How to print a list without quotes in Python?
Given a Python list of strings such as ['1', '2', '3']
.
If you print the list to the shell using print(['1', '2', '3'])
, the output is enclosed in square brackets and the strings have quotes like so: "
.['1', '2', '3']
"
This article will show you how to print the list in three variants:
- without quotation marks.
- without quotation marks and commas.
- without quotation marks, commas, and brackets.
print(['1', '2', '3']) # Output: ['1', '2', '3'] ''' Desired Output: 1. [1, 2, 3] 2. [1 2 3] 3. 1 2 3 '''
Let’s start with the easiest way to accomplish the first right away!
Method 1: Print List Without Quotation Marks
To print a string list without quotes, use the expression '[' + ', '.join(lst) + ']'
to create a single string representation of the list without the quotes around the individual strings.
Here’s an example:
# Print List Without Quotes lst = ['1', '2', '3'] # Create List Representation s = '[' + ', '.join(lst) + ']' # Print List print(s) # [1, 2, 3]
We use the following Python features. Check out the articles if you need background on any of those:
- String Concatenation to glue together the opening and closing brackets and the string elements without quotes.
- The Python String
join()
method to create one big string from the list of strings.
Method 2: Print List Without Quotes and Commas
To print a string list without quotes and commas, use the expression '[' + ' '.join(lst) + ']'
to create a single string representation of the list without the quotes around the individual strings and without the commas separating the list elements. You can change the separator string to your need (e.g., joining on the tabular character).
Here’s an example:
# Print List Without Quotes and Commas lst = ['1', '2', '3'] # Create List Representation s = '[' + ' '.join(lst) + ']' # Print List print(s) # [1 2 3]
Method 3: Print List Without Quotes, Commas, and Brackets
To print a string list without quotes and commas and brackets, use the ' '.join(lst)
method to create a single string representation of the list. You can change the separator string to your need (e.g., joining on the tabular character ', '.join(lst)
).
Here’s an example:
# Print List Without Quotes lst = ['1', '2', '3'] # Create List Representation s = ' '.join(lst) # Print List print(s) # 1 2 3
By the way, there are many more ways to print a list. You may also be interested in this article on the Finxter blog:
π Recommended Tutorial: How to Print a List Without Brackets in Python?
Bonus 1: print() Function and Unpacking
The asterisk operator *
is used to unpack an iterable into the argument list of a given function.
You can unpack all list elements into the print()
function to print all values individually, separated by an empty space per default (that you can override using the sep
argument). For example, the expression print(*my_list)
prints the elements in my_list
, empty space separated, without the enclosing square brackets and without the separating commas!
Here’s an example:
my_list = [1, 2, 3] print(*my_list) # Output: 1 2 3
βΉοΈ Note: If you want a different separating character, you can set the sep
argument of the print()
function. For example, print(*my_list, sep='|')
will use the vertical bar '|'
as a separating character.
Here’s an example:
my_list = [1, 2, 3] print(*my_list, sep='|') # Output: 1|2|3
You can learn about the ins and outs of the built-in print()
function in the following video:
Bonus 2: Convert String to Integer List
If you want to print a list without the quotes, you can convert the string list to an integer list and use the asterisk operator to unpack all integers into the print()
function like so: print(*[int(x) for x in lst], sep=' ')
.
Here’s an example:
# Print String List lst = ['1', '2', '3'] print(*[int(x) for x in lst], sep=' ') # [1 2 3]
To master the basics of unpacking, feel free to check out this video on the asterisk operator:
Programmer Humor
β Question: How did the programmer die in the shower? β οΈ
β Answer: They read the shampoo bottle instructions:
Lather. Rinse. Repeat.