9.11. Studio: Strings and Lists

Strings are ordered collections of characters, which are strings of length 1. The characters in a string can be accessed using bracket notation.

Lists are ordered collections of items, which can be strings, numbers, other lists, etc. The items/elements/entries stored in an list can be accessed using bracket notation.

Strings are immutable, whereas lists can be changed.

Strings and lists have properties and methods that allow us to easily perform some useful actions.

9.11.1. Part 1: String Modification

Use string methods to convert a word into pseudo-pig latin.

  1. Remove the first three characters from a string and add them to the end. Ex: 'LaunchCode' becomes 'nchCodeLau'. Use a template literal to print the original and modified string in a descriptive phrase.

  2. Modify your code to accept user input. Query the user to enter the number of letters that will be relocated.

  3. Add validation to your code to deal with user inputs that are longer than the word. In such cases, default to moving 3 characters. Also, the template literal should note the error.

Code it at repl.it

9.11.2. Part 2: List and String Conversion

The split and join methods convert back and forth between strings and lists. Use delimiters as reference points to split a string into an list, then modify the list and convert it back to a printable string.

  1. For a given string, use the in method to check to see if the words are separated by commas (,), semicolons (;) or just spaces.

  2. If the string uses commas to separate the words, split it into an list, reverse the entries, and then join the list into a new comma separated string. For example, "up,to,code,fun" becomes "fun,code,to,up".

  3. If the string uses semicolons to separate the words, split it into an list, alphabetize the entries, and then join the list into a new hyphen separated string. For example, "up;to;code;fun" becomes "code-fun-to-up".

  4. If the string uses spaces to separate the words, split it into an list, reverse alphabetize the entries, and then join the list into a new space separated string. For example, "to code up fun" becomes "up to fun code".

  5. Consider: What if the string uses ‘comma spaces’ (, ) to separate the list? Modify your code to produce the same result as part “b”, making sure that the extra spaces are NOT part of the final string.

Code it at repl.it

9.11.3. Part 3: Optional Fun With Multi-dimensional Lists

Lists can store other lists!

  1. The cargo hold in our shuttle contains several smaller storage spaces. Use split to convert the following strings into four cabinet lists. Alphabetize the contents of each cabinet.

    1. "water bottles, meal packs, snacks, chocolate"

    2. "space suits, jet packs, tool belts, thermal detonators"

    3. "parrots, cats, moose, alien eggs"

    4. "blankets, pillows, eyepatches, alarm clocks"

  2. Initialize a cargoHold list and add the cabinet lists to it. Print cargoHold to verify its structure.

  3. Query the user to select a cabinet (0-3) in the cargoHold.

  4. Use bracket notation and a template literal to display the contents of the selected cabinet. If the user entered an invalid number, print an error message instead.

  5. Bonus to the Bonus: Modify the code to query the user for BOTH a cabinet in cargoHold AND a particular item. Use the includes method to check if the cabinet contains the selected item, then print "Cabinet ____ DOES/DOES NOT contain ____."

Code it at repl.it

9.11.4. Submitting Your Work

You should have 2 repls when finished with the studio. Copy the URLs to your repls, separating each URL with a semi-colon and paste them into the submission box in Canvas for Studio: Strings and Lists and click Submit.

If you did Part 3, submit that through Canvas as well.