Converting Text to HTML - Python Programming Exercise

In this exercise, you will develop and program a Python class "TextToHTML" that can convert multiple texts entered by the user into a sequence of HTML lines. This exercise is perfect for practicing class definition, method implementation, and string manipulation in Python. By implementing this class, you will gain hands-on experience in handling class definitions, method implementation, and string manipulation in Python. This exercise not only reinforces your understanding of object-oriented programming but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Python Classes in OOP

 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

 Copy 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

 More Python Programming Exercises of Mastering Python Classes in OOP

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.

  •  Text on Screen Class

    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...

  •  Improved ComplexNumber Class

    In this exercise, you will develop a Python program to enhance the "ComplexNumber" class by overloading the addition (+) and subtraction (-) operators. This exerci...

  •  3D Coordinates

    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 ...

  •  Catalog & Navigation

    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 ...

  •  Object array: tables

    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...

  •  House

    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...