Last updated: Apr 9, 2024
Reading timeยท5 min
To print a list without the commas and brackets:
str.join()
method to join the list into a string.print()
function to print the string.list_of_strings = ['bobby', 'hadz', '.com'] # โ Print a list of strings without commas, brackets and quotes result = ''.join(list_of_strings) print(result) # ๐๏ธ bobbyhadz.com # --------------------------------------------- # โ Print a list of integers without commas, brackets and quotes list_of_integers = [7, 21, 44] result = ''.join(str(item) for item in list_of_integers) print(result) # ๐๏ธ 72144
We used the str.join()
method to print a list without the commas, brackets and
quotes.
The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
Note that the method raises a TypeError
if there are any non-string values in
the iterable.
If your list contains numbers or other types, convert all of the values to
strings before calling join()
.
list_of_integers = [7, 21, 44] result = ''.join(str(item) for item in list_of_integers) print(result) # ๐๏ธ 72144
We used a generator expression to iterate over the list.
On each iteration, we use the str() class to convert the number to a string.
The string the join()
method is called on is used as the separator between the
elements.
list_of_strings = ['bobby', 'hadz', '.com'] result = ' '.join(list_of_strings) print(result) # ๐๏ธ bobby hadz .com # --------------------------------------------- list_of_integers = [7, 21, 44] result = ' '.join(str(item) for item in list_of_integers) print(result) # ๐๏ธ 7 21 44
If you don't need a separator and just want to join the list's elements into a
string, call the join()
method on an empty string.
You can also use the map()
function to convert all items in the list to
strings before calling join()
.
list_of_integers = [7, 21, 44] result = ''.join(map(str, list_of_integers)) print(result) # ๐๏ธ 72144
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
Alternatively, you can use the sep
argument in the call to the
print() function.
The items in the list will get unpacked in the call to the print()
function
and will get printed without commas, brackets and quotes.
list_of_strings = ['bobby', 'hadz', '.com'] print(*list_of_strings, sep='') # ๐๏ธ bobbyhadz.com # --------------------------------------------- list_of_integers = [7, 21, 44] print(*list_of_integers, sep='') # ๐๏ธ 72144
*
operator to unpack the items of the list in the call to print()
.The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.
The sep
argument is the separator between the arguments we pass to print()
.
list_of_strings = ['bobby', 'hadz', '.com'] print(*list_of_strings, sep='') # ๐๏ธ bobbyhadz.com print(*list_of_strings, sep=' ') # ๐๏ธ bobby hadz .com print(*list_of_strings, sep='-') # ๐๏ธ bobby-hadz-.com
By default, the sep
argument is set to a space.
If you only need to print a list without the brackets:
str.join()
method to join the list into a string.print()
function to print the string.list_of_strings = ['bobby', 'hadz', 'com'] # โ Print a list of strings without brackets result = ', '.join(list_of_strings) print(result) # ๐๏ธ bobby, hadz, com # --------------------------------------------- # โ Print a list of numbers without brackets list_of_numbers = [11, 33, 55] result = ', '.join(str(item) for item in list_of_numbers) print(result) # ๐๏ธ 11, 33, 55
We used the str.join()
method to print a list without square brackets.
The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
Note that the method raises a TypeError
if there are any non-string values in
the iterable.
If your list contains numbers or other types, convert all of the values to
strings before calling join()
.
list_of_numbers = [11, 33, 55] result = ', '.join(str(item) for item in list_of_numbers) print(result) # ๐๏ธ 11, 33, 55
We used a generator expression to iterate over the list.
On each iteration, we use the str()
class to convert the number to a string.
The string the join()
method is called on is used as the separator between the
elements.
list_of_strings = ['bobby', 'hadz', 'com'] result = ' '.join(list_of_strings) print(result) # ๐๏ธ bobby hadz com
If you don't need a separator and just want to join the list's elements into a
string, call the join()
method on an empty string.
list_of_numbers = [11, 33, 55] result = ''.join(str(item) for item in list_of_numbers) print(result) # ๐๏ธ 113355
Alternatively, you can use the sep
argument in the call to the print()
function.
list_of_strings = ['bobby', 'hadz', 'com'] # ๐๏ธ bobby, hadz, com print(*list_of_strings, sep=', ') # ---------------------------------------------- list_of_numbers = [11, 33, 55] # ๐๏ธ 11, 33, 55 print(*list_of_numbers, sep=', ')
*
operator to unpack the items of the list in the call to print()
.The * iterable unpacking operator enables us to unpack an iterable in function calls, in comprehensions and in generator expressions.
The sep
argument is the separator between the arguments we pass to print()
.
print('bobby', 'hadz', sep='') # ๐๏ธ bobbyhadz print('bobby', 'hadz') # ๐๏ธ bobby hadz
By default, the sep
argument is set to a space.
A less flexible approach is to convert the list to a string and use string slicing.
list_of_strings = ['bobby', 'hadz', 'com'] # ๐๏ธ 'bobby', 'hadz', 'com' print(str(list_of_strings)[1:-1]) # --------------------------------------------- list_of_numbers = [11, 33, 55] # ๐๏ธ 11, 33, 55 print(str(list_of_numbers)[1:-1])
We used the str()
class to convert the list to a string and used string
slicing to exclude the square brackets.
The syntax for string slicing
is my_str[start:stop:step]
.
start
index is inclusive, whereas the stop
index is exclusive (up to, but not including).Python indexes are zero-based, so the first character in a string has an index
of 0
, and the last character has an index of -1
or len(my_str) - 1
.
We used a start
index of 1
to exclude the left square bracket and used a
stop
index of -1
to exclude the right square bracket.
I've also written an article on how to print a list in columns.
You can learn more about the related topics by checking out the following tutorials: