Write A Python Function To Get A String Which Is N (Non-Negative Integer) Copies Of A Given String

User inputs a string and number to the python program. Task is to get the n number of copies of string.

Python function you should return the n copies of the given string. There are two ways to solve this program first is using for loop (iteration) and next is using string multiply

Let's understand both solutions.

 

Write A Python Function To Get A String Which Is N (Non-Negative Integer) Copies Of A Given String

 




Write A Python Function To Get A String Which Is N (Non-Negative Integer) Copies Of A Given String

Take a function n_copies() the input arguments to the function is a string and a number.  take a new string variable and using the for loop iterate up to n,  keep adding the given string to your string up to n.

Once the for loop completes the new string will contain the n copies of a given string so return the new string.

def n_copies(my_string, n):
    new_string = ""
    for _ in range(n):
        new_string = new_string + my_string + "\n"
    return new_string





In the main program take a string and integer variable and ask a user to enter a string and integer (i.e, N copies of string).

Once we get a string and integer values call the function directly inside a print statement.  this will bring the 10 copies of a given input string.

data = input("Enter a string  : ")
number = int(input("Enter the value of n : "))
print(n_copies(data, number))






Complete program
=========================

def n_copies(my_string, n):
    new_string = ""
    for _ in range(n):
        new_string = new_string + my_string + "\n"
    return new_string


data = input("Enter a string  : ")
number = int(input("Enter the value of n : "))
print(n_copies(data, number))

 




OUTPUT
==================

Enter a string  : hello
Enter the value of n : 10
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello





Write A Python Function To Get A String Which Is N (Non-Negative Integer) Copies Of A Given String

Here is a second method to solve this Python program using the string multiplication operation in the function: multiply the given string by N and return the new string. As shown below

def n_copies(my_string, n):
    new_string = (my_string + "\n") * n
    return new_string





In the main program follow the above mentioned procedure to ask the user to enter a string and a number and directly call the function.

data = input("Enter a string  : ")
number = int(input("Enter the value of n : "))
print(n_copies(data, number))




Complete program
===================

def n_copies(my_string, n):
    new_string = (my_string + "\n") * n
    return new_string


data = input("Enter a string  : ")
number = int(input("Enter the value of n : "))
print(n_copies(data, number))





OUTPUT
=================

Enter a string  : this is a string
Enter the value of n : 4
this is a string
this is a string
this is a string
this is a string 





Conclusion
==================

These are the two methods to solve this program you can also use while loop for the iteration function.

Comment down below if you have any other method to solve this program.

 



Post a Comment

0 Comments