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! While you're at it, check out some resources Treehouse students have shared here.

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

Python

Why and when do we use a : when joining?

Ex. "My favorite flavors are: {}".join

Awesome ! Thanks for the help.

1 Answer

Ryan S
Ryan S
27,276 Points

Hi Chad,

In your example, the colon is just part of the string. It has nothing to do with .join().

However, there seems to be a bit of confusion in your use of the .join() method. It is used a bit differently then something like .split()

With .split(), you add it on to the end of a string (similar to what you did in your example with .join()). Inside the parentheses, you can specify what character you want the string to split at. If you leave it blank, it will split at the white space.

For example:

>>> "name:age:address".split(':')

['name', 'age', 'address']  # notice that the colon you chose as the break character has disappeared.

If the string only contains white space between the words:

>>> "name age address".split()

['name', 'age', 'address']

The .join() method is used in the opposite way. You need to specify the character that you want to join the list items with, and add .join() onto that. You would put the items you want to join inside the parentheses.

Example:

>>> ":".join(['name', 'age', 'address'])

"name:age:address"

If you used it the way you tried in your example, it would produce something quite different than what you intended:

>>> "My favorite flavors are: {}".join(['name', 'age', 'address'])

"nameMy favorite flavors are: {}ageMy favorite flavors are: {}address"

So back to your question about the colon, you would only specify a colon if you wanted to join your list items with a colon. But really you could use any character or combination of characters. It completely depends on what you want to do.

I hope this clears things up.