easy +10 pts

Remove HTML tags

Strip all HTML tags from a string while preserving the text content.

Write a function `strip_tags(html: str) -> str` that takes a string containing HTML markup and returns the text content with all HTML tags removed. A tag is defined as any substring starting with `<` and ending with the next `>`, inclusive. You should remove all such substrings. Everything outside tags (text) should remain unchanged, including spaces, newlines, and special characters like `&`. For example, given `'<h1>Hello</h1>'`, the function returns `'Hello'`. The input may contain nested tags, multiple tags, tags with attributes, empty tags, and might not contain any tags at all. You can assume all opening `<` characters are part of a valid tag (i.e., there is always a matching `>` later).

Constraints

Input string length: 0 ≤ len(html) ≤ 10^5. The input may contain any printable ASCII characters. You can assume the input is well-formed (every `<` has a following `>`). Time complexity: O(n) where n is the length of the input, because the expected solution processes each character once.

Example

>>> strip_tags('<h1>Hello</h1>')
'Hello'
>>> strip_tags('<p>Hello <b>world</b>!</p>')
'Hello world!'
>>> strip_tags('<a href="https://example.com">Link</a>')
'Link'
>>> strip_tags('No tags here')
'No tags here'
10 points ~15 min

Recent Submissions

No submissions yet — hit Run Tests to try!

Hints

Think of scanning the string character by character and deciding whether to copy each character to the output.
Use a flag to indicate whether you are currently inside a tag.
When you encounter '<', set the flag to True; when you encounter '>', set it to False.
Only copy characters to the result when the flag is False.
Python 3
All tests passed!
Test Results
Press Ctrl+Enter or click Run Tests to execute your code.