Python: Create the HTML string with tags around the word(s)
Python String: Exercise-15 with Solution
Write a Python function to create an 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>'
Sample Solution:
Python Code:
# Define a function named add_tags that takes two arguments, 'tag' and 'word'.
def add_tags(tag, word):
# Use string formatting to create and return a new string with the provided 'tag' enclosing the 'word'.
return "<%s>%s</%s>" % (tag, word, tag)
# Call the add_tags function with the tag 'i' and the word 'Python' and print the result.
print(add_tags('i', 'Python'))
# Call the add_tags function with the tag 'b' and the word 'Python Tutorial' and print the result.
print(add_tags('b', 'Python Tutorial'))
Sample Output:
<i>Python</i> <b>Python Tutorial</b>
Flowchart:
Python Code Editor:
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.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/python-exercises/string/python-data-type-string-exercise-15.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics