Strings: making HTML tags: Difference between revisions

From Computer Science Wiki
Line 31: Line 31:




<div class="toccolours mw-collapsible">
<div class="toccolours mw-collapsible mw-collapsed">
 
Click here to see one possible solution, but before you have tried and failed!
 
<div class="mw-collapsible-content">
<syntaxhighlight lang="python" line="1" >
<syntaxhighlight lang="python" line="1" >


Line 55: Line 55:
</syntaxhighlight>
</syntaxhighlight>


 
</div>
</div>
</div>



Revision as of 12:09, 15 March 2016

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 understanding 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 [1]

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>

one possible solution

Click here to see one possible solution, but 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>

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

References