Write a program to repeat the string “GOOD MORNING” n times. Here ‘n’ is an integer entered by the user.
Program to repeat the string "GOOD MORNING" n times
Objective: To accept an integer n from user and display the string repeated n times
Accepting integer input from user
n = int(input("Enter the number of times to repeat: "))
Repeating the string using multiplication operator
result = "GOOD MORNING " * n
Displaying the output
print(result)
Example Output:
Enter the number of times to repeat: 3
GOOD MORNING GOOD MORNING GOOD MORNING
Explanation
This question tests the student's understanding of string operations in Python, specifically string multiplication. The program uses the string multiplication operator (*) which repeats the string n times. The input() function accepts user input as a string, which is then converted to an integer using int(). The expression "GOOD MORNING " * n creates a new string with the original string repeated n times. This is a fundamental string operation covered in NCERT Class 11 Computer Science under string handling concepts.
Solution Steps
Step 1: Accept integer input from user using int(input())
Step 2: Use string multiplication operator (*) to repeat the string n times
Step 3: Store the result in a variable
Step 4: Display the output using print() function