--- id: 68ffb91507a5b645769328c4 title: "Challenge 101: Markdown Heading Converter" challengeType: 29 dashedName: challenge-101 --- # --description-- Given a string representing a Markdown heading, return the equivalent HTML heading. A valid Markdown heading must: - Start with zero or more spaces, followed by - 1 to 6 hash characters (`#`) in a row, then - At least one space. And finally, - The heading text. The number of hash symbols determines the heading level. For example, one hash symbol corresponds to an `h1` tag, and six hash symbols correspond to an `h6` tag. If the given string doesn't have the exact format above, return `"Invalid format"`. For example, given `"# My level 1 heading"`, return `"

My level 1 heading

"`. Note: The console may not display HTML tags in strings when logging messages. Check the browser console to see logs with tags included. # --hints-- `convert("# My level 1 heading")` should return `"

My level 1 heading

"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(convert("# My level 1 heading"), "

My level 1 heading

")`) }}) ``` `convert("My heading")` should return `"Invalid format"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(convert("My heading"), "Invalid format")`) }}) ``` `convert("##### My level 5 heading")` should return `"
My level 5 heading
"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(convert("##### My level 5 heading"), "
My level 5 heading
")`) }}) ``` `convert("#My heading")` should return `"Invalid format"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(convert("#My heading"), "Invalid format")`) }}) ``` `convert(" ### My level 3 heading")` should return `"

My level 3 heading

"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(convert(" ### My level 3 heading"), "

My level 3 heading

")`) }}) ``` `convert("####### My level 7 heading")` should return `"Invalid format"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(convert("####### My level 7 heading"), "Invalid format")`) }}) ``` `convert("## My #2 heading")` should return `"

My #2 heading

"`. ```js ({test: () => { runPython(` from unittest import TestCase TestCase().assertEqual(convert("## My #2 heading"), "

My #2 heading

")`) }}) ``` # --seed-- ## --seed-contents-- ```py def convert(heading): return heading ``` # --solutions-- ```py import re def convert(heading): trimmed = heading.strip() if not re.match(r'^#{1,6}\s+\S+', trimmed): return "Invalid format" hash_count = trimmed.find(' ') text = trimmed[hash_count:].lstrip() return f"{text}" ```