How do `split` and `join` turn a sentence into a list and back — making word-counting a two-line job?
Short drills on what this video just taught. Write the code, run the checks, and reveal the answer only if you are stuck.
Casting a string to a list turns every character — letters, spaces and symbols — into its own element. Turn text into a list of its individual characters and assign it to chars.
Hint: list() casts a string into a list of its characters.
list(s) against s.split(" ")
Write what list("I heart cs &u") produces character by character, then what splitting the same string on a space produces, and note that any character can serve as the separator.
"sep".join(L) and its string-only requirement
Record "".join(["A", "B", "C"]) giving "ABC" and "_".join(L) giving "A_B_C", and state that a list holding integers, floats or Booleans raises an error.
Casting elements before joining
State that non-string elements must first be cast to strings in a loop, and show the numbers 1, 2 and 3 turned into the string "123".
count_words(sen) in two lines
Write L1 = sen.split(' ') then return len(L1), and record the counts 3 and 12 that the two sample sentences produce.