w3resource

Python: Create the HTML string with tags around the word(s)

Python String: Exercise-15 with Solution

Write a Python function to create the HTML string with tags around the word(s).
Sample function and result :
add_tags('i', 'Python') -> '<i>Python</i>'
add_tags('b', 'Python Tutorial') -> '<b>Python Tutorial </b>'

Python String Exercises: Create the HTML string with tags around the word(s)

Sample Solution:-

Python Code:

def add_tags(tag, word):
	return "<%s>%s</%s>" % (tag, word, tag)
print(add_tags('i', 'Python'))
print(add_tags('b', 'Python Tutorial'))

Sample Output:

Python                                                                                                 
Python Tutorial 

Flowchart:

Flowchart: Create the HTML string with tags around the word(s)

Python Code Editor:

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Python program that accepts a comma separated sequence of words as input and prints the unique words in sorted form (alphanumerically).
Next: Write a Python function to insert a string in the middle of a string.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Performs right-to-left function composition:

Example:

from functools import reduce

def tips_compose(*fns):
  return reduce(lambda f, g: lambda *args: f(g(*args)), fns)
add6 = lambda x: x + 6
multiply = lambda x, y: x * y
multiply_and_add_6 = tips_compose(add6, multiply)

print(multiply_and_add_6(10, 3))

Output:

36