Strings: making HTML tags

From Computer Science Wiki
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.
This a problem set for you to work through [1]

This is a problem set. Some of these are easy, others are far more difficult. The purpose of these problems sets are to HELP YOU THINK THROUGH problems. The solution is at the bottom of this page, but please don't look at it until you have tried (and failed) at least three or four times.

The Problem

This problem is designed to test your skill and knowledge of strings, specifically your knowledge and understanding of combining (concatenating) strings.

The web is built with HTML strings like "<i>Yay</i>" which draws Yay as italic text. In this example, the "i" tag makes and which surround the word "Yay". Given tag and word strings, create the HTML string with tags around the word [2]

Some Code to Get You Started

#
# this function returns a HTML opening and closing pair of tag
#

def make_tags(tag, word):

    return

# example input: 
# make_tags('i','Hello world')

# example output:
# <i>Hello world</i>

Take This Farther

This simple function is very useful, and it is likely that you have used a function like this to add bold face text to a google doc.

  • Please create a function that creates a link. <a href="http://reddit.com/r/python">Click here</a>. You will need the link and text to display to the user.
  • Add some logic which rejects invalid HTML tags

How you will be assessed

Every problem set is a formative assignment. Please click here to see how you will be graded

References

One Possible Solution

Click the expand link to see one possible solution, but NOT before you have tried and failed!

#
# this function returns a HTML opening and closing pair of tag
#

def make_tags(tag, word):

    print("<" + tag + ">" + word + "</" + tag + ">")

    return

# example input: 
# make_tags('i','Hello world')
 
# example output:
# <i>Hello world</i>