tags_equal

Python Morsels Exercise

Determine whether two HTML tags are equivalent

3 bonuses 11 hints 21 solutions 299 users solved 20 reviews

This exercise might seem a bit uninteresting at first because it involves working with an HTML-like syntax at a low level. The purpose for this exercise isn't to familiarize yourself with HTML, but to get some practice with string manipulation in Python.

I'd like you to write a function that accepts two strings containing opening HTML tags and returns True if they have the same attributes with the same values.

Some examples of basic tag comparisons I'd like you to handle:

>>> tags_equal("<img src=cats.jpg height=40>", "<IMG SRC=cats.jpg height=40>")
True
>>> tags_equal("<img src=dogs.jpg width=99>", "<img src=dogs.jpg width=20>")
False
>>> tags_equal("<p>", "<P>")
True
>>> tags_equal("<b>", "<p>")
False

This might sound complex and it sort of is.

To make things a little easier:

  1. Assume attributes don't have double/single quotes around them and don't contain spaces (until you get bonus 3)
  2. Don't worry about repeated attribute names or value-less attributes. Assume there won't be repeats (until you get to bonus 1)
  3. Assume all attributes are key-value pairs (until you get to bonus 2)
  4. Assume attributes have no whitespace around them (key=value and never key = value)

But your function must:

  1. Ignore order of attributes: the same attribute names/values in a different order should be equivalent
  2. Ignore case for both attribute names and values (yes even ignore case for attribute values)

I encourage you to try solving this exercise without using the standard library at first. Everything but the last bonus should be relatively do-able without importing any libraries.

If you get your tests to pass, consider doing some of these bonuses. Make sure you don't spend too much time trying to get the second or third bonus done though.

Bonus 1


This is just a preview of the problem statement.

This exercise includes 3 bonuses and 11 hint links.

To solve this exercise, sign in to your Python Morsels account.

A learning habit for experienced Pythonistas

Profile picture of Trey

My name is Trey Hunner and I hold Python trainings for teams. I've spent countless hours refining my most interesting Python exercises into Python Morsels, a Python skill-building platform for folks who already know Python. Python Morsels is design to help you deepen your Python skills every week.

Sign up for Python Morsels to access this exercise and many more.

Join Python Morsels ✨