Print a list without brackets in Python

In this article, we will discuss different ways to print a list without brackets in Python.

Table Of Contents

Method 1: Using print() function and astrik

Apply astrik to a list object, to convert all the list elements to individual string objects. Then pass them to the print() function, along with a comma as a separator. It will print all the list elements on console without bracket. Let’s see an example,

listOfStrs = ['Hello', 'this', 'is', 'a', 'correct', 'way', 'to', 'code']

# print list elements without brackets
print(*listOfStrs, sep=', ')

Output:

Hello, this, is, a, correct, way, to, code

Method 2: By converting into string

Convert list to string, and select all characters from string except first and last character. This way we can print the list contents without brackets in Python. Let’s see an example,

Advertisements
listOfStrs = ['Hello', 'this', 'is', 'a', 'correct', 'way', 'to', 'code']

# print list elements without brackets
print(str(listOfStrs)[1:-1])

Output:

Hello, this, is, a, correct, way, to, code

Method 3: Using join() function

Example 1

Join all the list elements to create a comma separated string using the join() function. To call the join() function, use the comma as string object. It will return a string containing all list elements without brackets. Then print that string. Let’s see an example,

listOfStrs = ['Hello', 'this', 'is', 'a', 'correct', 'way', 'to', 'code']

# convert list to string
strValue = ', '.join(listOfStrs)

# print list elements without brackets
print(strValue)

Output:

Hello, this, is, a, correct, way, to, code

Example 2

If you have a list, in which few elements are not string. Then you need to first convert them to a string before passing them to the join() function. Then the join() function will convert them to string. Let’s see an example,

listOfInts = [11, 12, 13, 14, 15, 16, 17]

# convert list to string
strValue = ', '.join(map(str, listOfInts))

# print list elements without brackets
print(strValue)

Output:

11, 12, 13, 14, 15, 16, 17

Summary

We learned about different ways to print list contents on console without brackets in Python.

Advertisements

Thanks for reading.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top