Create multiple copies of a string in Python by using multiplication operator
In this article, we will see how to create multiple copies of a string by using the multiplication operator(*). Python supports certain operations to be performed on a string, multiplication operator is one of them.
Simply using multiplication operator on the string to be copied with the required number of times it should be copied.
str2 = str1 * N
where str2 is the new string where you want to store the new string
str1 is the original string
N is the number of the times you want to copy the string.
After using multiplication operator we get a string as output
Orignal string is: Geeks
New string is: GeeksGeeksGeeks
Original string is: Hello New string is: HelloHelloHelloHelloHello
Orignal string is: Hello
New string is: HelloHelloHelloHelloHello
Method 2: Copying a string multiple times given in a list
If we have a string as a list element, and we use the multiplication operator on the list we will get a new list that contains the same element copied specified number of times.
a = [“str1”] * N
a will be a list that contains str1 N number of times.
It is not necessary that the element we want to duplicate in a list has to be a string. Multiplication operator in a list can duplicate anything.
Original list is: [‘Geeks’]
List after multiplication is: [‘Geeks’, ‘Geeks’, ‘Geeks’]
Example 4: Shorthand method for the same approach
[True, True, True, True, True]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course