curriculum/challenges/english/blocks/daily-coding-challenges-python/68ffb91507a5b645769328c4.md
Given a string representing a Markdown heading, return the equivalent HTML heading.
A valid Markdown heading must:
#) in a row, thenThe 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 "<h1>My level 1 heading</h1>".
Note: The console may not display HTML tags in strings when logging messages. Check the browser console to see logs with tags included.
convert("# My level 1 heading") should return "<h1>My level 1 heading</h1>".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(convert("# My level 1 heading"), "<h1>My level 1 heading</h1>")`)
}})
convert("My heading") should return "Invalid format".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(convert("My heading"), "Invalid format")`)
}})
convert("##### My level 5 heading") should return "<h5>My level 5 heading</h5>".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(convert("##### My level 5 heading"), "<h5>My level 5 heading</h5>")`)
}})
convert("#My heading") should return "Invalid format".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(convert("#My heading"), "Invalid format")`)
}})
convert(" ### My level 3 heading") should return "<h3>My level 3 heading</h3>".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(convert(" ### My level 3 heading"), "<h3>My level 3 heading</h3>")`)
}})
convert("####### My level 7 heading") should return "Invalid format".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(convert("####### My level 7 heading"), "Invalid format")`)
}})
convert("## My #2 heading") should return "<h2>My #2 heading</h2>".
({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(convert("## My #2 heading"), "<h2>My #2 heading</h2>")`)
}})
def convert(heading):
return heading
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"<h{hash_count}>{text}</h{hash_count}>"