Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community!

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


How do I print a list without brackets?

Suppose I have the following code:

animals = [lion, tiger, bear]
print("Animals:  {}".format(animals))

The output will be: "Animals: [lion, tiger, bear]"

How do I remove the brackets?

4 Answers

Hey Henry, when you print out a list it is going to print out as a list, not as a string. To accomplish what you are trying to do you would first need to create a string made up of the items in your list. Luckily there is a method that can take care of that for you easily. To print out animals as a string, you need to do this: print(", ".join(animals)).

The string.join() method will take a string and join a list with it using the spaces and characters that you specify. Try out the code snippet I provided. Notice that I used ", ".join(...), and this creates the string "lion, tiger, bear" from your list. That's because I called .join() on a string with a single empty space and a comma in it, so join will insert a comma and an empty space between each list item.

I encourage you to read up on up string.join(). I hope this explanation was clear! Happy coding.

Thanks. How would I do the same with a list of numbers?

For example:

num = [1, 2, 3]
print(", ".join(num))

This returns: TypeError: sequence item 0: expected str instance, int found

You would need to either first coerce each integer in the list to a string type (not recommended, very inefficient), or you could create your list as a list of strings containing numbers: num = ['1', '2', '3']. The string.join() method is a string method, it takes different strings and basically concatenates them for you using the sort of spacing you want. ", ".join(num) will work just fine as long as it is joining strings. :)

I should point out that in your original post the code you included doesn't have your list items as strings. I definitely recommend enclosing any and all objects that are intended as strings in quotation marks, single or double. It will prevent you from getting annoying errors when you run your code!

I found an alternative. How is this:

num = [1, 2, 3]
print(", ".join(map(str, num)))

It seems to work.


Yes, I noticed that the original code did not enclose the animals in quotes. Typo.