PCAP Strings • Set 9
PCAP Strings Practice Test 9 — 15 questions with explanations. Free, no signup.
A junior developer is building a script to convert user-provided headlines into URL slugs. The slug should contain only lowercase alphanumeric characters and single hyphens between words, with no leading or trailing hyphens. For example, 'Hello World! How are you?' should become 'hello-world-how-are-you'. The current code is: slug = input_string.lower().replace(' ', '-').replace('!', '').replace('?', ''). However, this produces multiple hyphens when there are multiple spaces, and trailing hyphens if the string ends with punctuation. The developer needs to modify the code to handle these issues reliably. Which of the following approaches is the most robust and efficient?
Choose an answer to begin — your selection is scored in the full session.
15 questions · instant feedback and full explanations after every question.