oldoc63 / learningDS

Learning DS with Codecademy and Books
0 stars 0 forks source link

Joining Strings #342

Open oldoc63 opened 1 year ago

oldoc63 commented 1 year ago

Now that you've learned to break strings apart using .split(), let learn to put them back together using .join(). join() is essentially the opposite of .split(), it joins a list of strings together with a given delimiter. The syntax of .join() is:

'delimiter'.join(list_you_want_to_join)

Now this may seem a little weird, because with .split() the argument was the delimiter, but now the argument is the list. This is because join is still a string method, which means, it has to act on a string. The string .join() acts on is the delimiter you want to join with, therefore the list you want to join has to be the argument.

oldoc63 commented 1 year ago

We take the list of strings, my_munequita, and we joined it together with our delimiter, ' ', which is a space. The space is important if you are trying to build a sentence from words, otherwise, we would have ended up with:

oldoc63 commented 1 year ago

You've been provided with a list of words from the first line of Jean Toomer's poem Reapers. Use .join() to combine these words into a sentence and save that sentence as the string reapers_line_one.

oldoc63 commented 1 year ago

In the last exercise, we joined together a list of words using a space as the delimiter to create a sentence. In fact, you can use any string as a delimiter to join together a list of strings. Often used is a comma , because then we can create a string of comma separated variables, or CSV.

oldoc63 commented 1 year ago

You'll often find data stored in CSVs because it is an efficient, simple file type used by popular programs like Excel or Google Spreadsheets. We can also join using escape sequences as the delimiter:

oldoc63 commented 1 year ago

You've been given a list, winter_trees_lines, that contains all the lines to William Carlos Williams poem, Winter Trees. You've been asked to join together the strings in the list into a single string that can be used to display the full poem. Name this string winter_trees_full. Print the result to the terminal. Make sure that each line of the poem appears on a new line in your string.