Strings: making HTML tags

From Computer Science Wiki
Jump to: navigation, search
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

 1 #
 2 # this function returns a HTML opening and closing pair of tag
 3 #
 4 
 5 def make_tags(tag, word):
 6 
 7     return
 8 
 9 # example input: 
10 # make_tags('i','Hello world')
11 
12 # example output:
13 # <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!

 1 #
 2 # this function returns a HTML opening and closing pair of tag
 3 #
 4 
 5 def make_tags(tag, word):
 6 
 7     print("<" + tag + ">" + word + "</" + tag + ">")
 8 
 9     return
10 
11 # example input: 
12 # make_tags('i','Hello world')
13  
14 # example output:
15 # <i>Hello world</i>