Deals Of The Week - hours only!Up to 80% off on all courses and bundles.-Close
Introduction
Quick recap
7. The join() function
Indexing and slicing
Summary

Instruction

Another interesting technique is to use join():

visited_countries = ['Norway', 'Sweden', 'Denmark', 'Germany', 'France']
separator = ', '
print('I have visited the following countries: ' + separator.join(visited_countries))

The code above will show:

I have visited the following countries: Norway, Sweden, Denmark, Germany, France

Here,

separator.join(visited_countries)
creates a string that contains all the elements of visited_countries separated by the separator. Note that there is no separator after the last element in the list.

Exercise

Join all path parts in the template with backslash signs (\) and print the entire string.

Remember—to access a backslash sign in Python, you need to write: '\\'

Stuck? Here's a hint!

Use '\\'.join(path_parts).