Open In App

How to Print a List Without Brackets in Python

Last Updated : 04 Dec, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In this article, we will see how we can print a list without brackets in Python. Whether we're formatting data for user interfaces, generating reports, or simply aiming for cleaner console output, there are several effective methods to print lists without brackets.

Using * Operator for Unpacking List Elements

The most simple and efficient method is using * operator to unpack the list elements when passing them to the print function. It prints the list elements separated by spaces without the need for additional formatting.

Python
li = [1, 2, 3, 4, 5]
print(*li)

Output
1 2 3 4 5

Below are some of the advanced approaches to print a list without brackets in Python.

Using join() Method (Specified Delimiter )

In this example, we are using join() method to concatenate the elements of a list into a single string separated by a specified delimiter.

Python
li = ['4', '5', '6']
print(', '.join(li))

Output
4, 5, 6

Explanation:

  • The code creates a list li containing the strings '4', '5', and '6'.
  • It then joins the list elements into a single string with , as the separator and prints 4, 5, 6.

Note: We can replace the delimiter ', ' with any delimiter we want in the join() method. The join() method does not have a default delimiter; it must be explicitly specified.

Using str() Function with String Slicing

The str() function can be combined with string slicing to remove the brackets from the list representation.

Python
li = [1, 2, 3, 4, 5]
print(str(li)[1:-1])

Output
1, 2, 3, 4, 5

Explanation:

  • The code converts the list li into a string and removes the first and last characters which are the square brackets.
  • It then prints the resulting string which is the list's elements separated by commas without the brackets.

Using For Loop

In this example, we are using a loop to iterate over the list elements and print them without brackets.

Python
li = [1, 2, 3, 4, 5]
for i in li:
    print(i, end=' ')

Output
1 2 3 4 5 


Next Article

Similar Reads