- Published on
Printing a list without brackets in Python
- Authors
- Name
- hwahyeon
- Using Unpacking with the
*
Operator In Python, you can unpack a list using the*
operator. This allows you to print all elements without brackets, as shown below:
lst = ['x','y','z']
print(*lst, sep = ',')
# Output x,y,z
- Using
str()
Function You can also use thestr()
function to convert a list to a string, and then slice off the brackets:
lst = [1,2,3]
lst_str = str(lst)[1:-1]
print(lst_str)
# Output 1, 2, 3
Note: When converting a list to a string using str()
, a space is automatically added after each comma between elements. This happens regardless of the type of the elements (e.g., int
, str
, or float
).