Exercise
Converting Text To HTML
Objective
Develop and program a Python class "TextToHTML" that can convert multiple texts entered by the user into a sequence of HTML lines. For example:
"Hello It's me I've finished"
should become:
`
Hello
It's me
I've finished
`
The class should include:
- An array to store strings
- A method "Add" to add a new string to the array
- A method "Display" to print the stored texts
- A method "ToString" to return a single string containing all the texts separated by new lines ("\n").
Additionally, create an auxiliary class with a "Main" function to test the "TextToHTML" class.
Example Python Exercise
Show Python Code
class TextToHTML:
def __init__(self):
"""
Initializes the TextToHTML class with an empty list to store text lines.
"""
self.texts = []
def Add(self, text):
"""
Adds a new string to the array of texts.
Parameters:
text (str): The text to be added to the array.
"""
self.texts.append(text)
def Display(self):
"""
Prints each stored text inside a tag.
"""
for text in self.texts:
print(f"
{text}
")
def ToString(self):
"""
Returns a single string containing all the texts separated by new lines.
Returns:
str: All texts joined by newline characters.
"""
return "\n".join(self.texts)
# Auxiliary class with a main function to test the TextToHTML class.
class Main:
@staticmethod
def run():
"""
Main function to test the TextToHTML class.
"""
# Create an instance of the TextToHTML class
text_to_html = TextToHTML()
# Add some example texts
text_to_html.Add("Hello")
text_to_html.Add("It's me")
text_to_html.Add("I've finished")
# Display the texts as HTML paragraphs
print("Displaying texts as HTML paragraphs:")
text_to_html.Display()
# Get the texts as a single string with new lines
print("\nDisplaying texts as a single string with new lines:")
print(text_to_html.ToString())
# Running the test
if __name__ == "__main__":
Main.run()
Output
Displaying texts as HTML paragraphs:
Hello
It's me
I've finished
Displaying texts as a single string with new lines:
Hello
It's me
I've finished
Share this Python Exercise
Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.
In this exercise, you will develop a Python class called "DisplayText" that allows you to show text at specific coordinates on the screen. This exercise is per...
In this exercise, you will develop a Python program to enhance the "ComplexNumber" class by overloading the addition (+) and subtraction (-) operators. This exerci...
In this exercise, you will develop a Python class "Point3D" that will represent a point in three-dimensional space with coordinates X, Y, and Z. This exercise ...
In this exercise, you will develop a Python program to enhance the Catalog application, where the "Main" function displays a menu that allows users to input new data ...
In this exercise, you will develop a Python class called "Table". The class should include a constructor to initialize the width and height of the table. It should al...
In this exercise, you will develop a Python program with the following classes: House: Create a class called "House" with an attribute for "area". Include a construc...