PCED-30-02

PCED-30-02 practice questions, page 2 of 2, with answer review and explanations.

Question 6

You are working with city names entered by users. These names may contain inconsistent capitalization and unwanted spaces. To standardize the data, you want to: Remove any leading or trailing whitespace, and Capitalize the first letter of each word (e.g., convert "new york" to "New York"). For example: "New york " → "New York" "lOS ANGELES" → "Los Angeles" You are given a variable city that contains the raw input. Which line of code correctly updates the value of cleaned_city to apply the required transformation? Select the best answer.

  1. cleaned_city = city.strip().capitalize()
  2. cleaned_city = city.upper().replace(" ", "")
  3. cleaned_city = city.upper().strip()
  4. cleaned_city = city.strip().title()