Given a string and the number n the task is to print the given string n number of times in Python.
Examples:
Example1:
Input:
Given string = 'BTechgeeks ' Given number of times = 11
Output:
Printing the given string [ BTechgeeks ] 11 number of times : BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks
Example2:
Input:
Given string = 'aplustopper ' Given number of times = 8
Output:
Printing the given string [ aplustopper ] 8 number of times : aplustopper aplustopper aplustopper aplustopper aplustopper aplustopper aplustopper aplustopper
Program to Print a String N Number of Times in Python
Below are the ways to print the given string n number of times in Python.
- Using For loop(Static Input)
- Using For loop(User Input)
- Using * Operator (Static Input)
- Using * Operator (User Input)
The best and excellent way to learn a java programming language is by practicing Simple Java Program Examples as it includes basic to advanced levels of concepts.
Method #1: Using For Loop (Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Give the number of times the string to be printed as static input and store it in another variable.
- Loop till a given number of times using the For loop.
- Inside the For loop print the given string using the print() statement.
- The Exit of the Program.
Below is the implementation:
# Give the string as static input and store it in a variable.
gvnstrng = 'BTechgeeks'
# Give the number of times the string to be printed
# as static input and store it in another variable.
numbtimes = 11
print('Printing the given string [', gvnstrng, ']', numbtimes,'number of times :')
# Loop till a given number of times using the For loop.
for times in range(numbtimes):
# Inside the For loop print the given string using the print() statement.
print(gvnstrng, end=' ')
Output:
Printing the given string [ BTechgeeks ] 11 number of times : BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks
Method #2: Using For Loop (User Input)
Approach:
- Give the string as user input using the input() function and store it in a variable.
- Give the number of times the string to be printed as user input using the int(input()) function and store it in another variable.
- Loop till a given number of times using the For loop.
- Inside the For loop print the given string using the print() statement.
- The Exit of the Program.
Below is the implementation:
# Give the string as user input using the input()
# function and store it in a variable.
gvnstrng = input('Enter some random string to be printed = ')
# Give the number of times the string to be printed as user input using the int(input()) function
# and store it in another variable.
numbtimes = int(
input('Enter some random number of times the string to be printed = '))
print('Printing the given string [', gvnstrng,
']', numbtimes, 'number of times :')
# Loop till a given number of times using the For loop.
for times in range(numbtimes):
# Inside the For loop print the given string using the print() statement.
print(gvnstrng, end=' ')
Output:
Enter some random string to be printed = AplusTopper Enter some random number of times the string to be printed = 8 Printing the given string [ AplusTopper ] 8 number of times : AplusTopper AplusTopper AplusTopper AplusTopper AplusTopper AplusTopper AplusTopper AplusTopper
Method #3: Using * Operator (Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Give the number of times the string to be printed as static input and store it in another variable.
- Print the given string given the number of times using the * operator.
- The Exit of the Program.
Below is the implementation:
# Give the string as static input and store it in a variable.
gvnstrng = 'BTechgeeks '
# Give the number of times the string to be printed
# as static input and store it in another variable.
numbtimes = 11
print('Printing the given string [', gvnstrng,
']', numbtimes, 'number of times :')
#Print the given string given the number of times using the * operator.
print(gvnstrng*numbtimes)
Output:
Printing the given string [ BTechgeeks ] 11 number of times : BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks BTechgeeks
Method #4: Using * Operator (User Input)
Approach:
- Give the string as user input using the input() function and store it in a variable.
- Give the number of times the string to be printed as user input using the int(input()) function and store it in another variable.
- Print the given string given the number of times using the * operator.
- The Exit of the Program.
Below is the implementation:
# Give the string as user input using the input()
# function and store it in a variable.
gvnstrng = input('Enter some random string to be printed = ')
# Give the number of times the string to be printed as user input using the int(input()) function
# and store it in another variable.
numbtimes = int(
input('Enter some random number of times the string to be printed = '))
print('Printing the given string [', gvnstrng,
']', numbtimes, 'number of times :')
# Print the given string given the number of times using the * operator.
print(gvnstrng*numbtimes)
Output:
Enter some random string to be printed = aplustopper Enter some random number of times the string to be printed = 8 Printing the given string [ aplustopper ] 8 number of times : aplustopper aplustopper aplustopper aplustopper aplustopper aplustopper aplustopper aplustopper
Related Programs:
- python program to print binary representation of a number
- python program to search the number of times a particular number occurs in a list
- python program to print all permutations of a string in lexicographic order without recursion
- python program to calculate the number of words and the number of characters present in a string
- python program to count number of lowercase characters in a string
- python program to calculate the number of digits and letters in a string
- python program to print each word of a sentence along with number of vowels in each word
