Let’s check out some exercises that will help you understand .join() method better.

Exercise 2-a: Join method of strings w/lists

Join the list's elements with: "+++".


“”.join(list) can be used to join elements of a list (or another iterable such as, tuples or dictionaries). Joining character goes inside the brackets and list goes inside the parenthesis.

joined = "+++".join(lst)

Exercise 2-b: Join method of strings w/tuples

Join the tuple's elements so that you get a proper email address.


“”.join(list) can be used to join elements of a list (or another iterable such as, tuples or dictionaries). Joining character goes inside the brackets and list goes inside the parenthesis.

email = "@".join(addresses)

Exercise 2-c: Join method with a special character

Join each element in the list with a space character: " "


“”.join(list) can be used to join elements of a list (or another iterable such as, tuples or dictionaries). Joining character goes inside the brackets and list goes inside the parenthesis.

str = " ".join(lst)

Exercise 2-d: Join method of strings w/dictionaries

Using .join() method join the keys in the dictionary with a comma: ",".


Although it might look slightly weird in the beginning, .format() method is an extremely convenient method for string class objects.

str = ",".join(economic_growth)

 

Exercise 2-e: Python join method with new line character

"\n" is a character that creates a new line. (Similar to when you press enter key in your text editor.)


Using "\n" character and .join() method, join the sentences in the list so that it looks like a proper poem.


Although it might look slightly weird in the beginning, .format() method is an extremely convenient method for string class objects.

poem_str = "\n".join(poem_lst)