Monday, 6 November 2023

Write a python class to accept a string and number n from user and display n repetition of strings by overloading * operator.

 

 Write a python class to accept a string and number n from user and display n repetition of strings by overloading * operator.    

 

class StringRepeater:

    def __init__(self, input_string):

        self.input_string = input_string

 

    def __mul__(self, n):

        if isinstance(n, int):

            return self.input_string * n

        else:

            raise ValueError("The repetition factor should be an integer")

 

# Get input from the user

user_input_string = input("Enter a string: ")

repetition_factor = int(input("Enter the repetition factor (an integer): "))

 

# Create an instance of the StringRepeater class

repeater = StringRepeater(user_input_string)

 

# Use the overloaded * operator to repeat the string

result = repeater * repetition_factor

 

# Display the repeated string

print("Result:", result)