Saturday, December 28, 2024
Google search engine
HomeLanguagesCreate multiple copies of a string in Python by using multiplication operator

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.

Method 1: 

Simply using multiplication operator on the string to be copied with the required number of times it should be copied.

Syntax:

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

Example 1:

Python3




# Original string
a = "Geeks"
 
# Multiply the string and store
# it in a new string
b = a*3
 
# Display the strings
print(f"Original string is: {a}")
print(f"New string is: {b}")


Output

Original string is: Geeks
New string is: GeeksGeeksGeeks

Example 2:

Python3




# Initializing the original string
a = "Hello"
n = 5
 
# Multiplying the string
b = a*n
 
# Print the strings
print(f"Original string is: {a}")
print(f"New string is: {b}")


Output

Original 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.

Syntax:

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.

Example 3:

Python3




# Initialize the list
a = ["Geeks"]
 
# Number of copies
n = 3
 
# Multiplying the list elements
b = a*n
 
# print the list
print(f"Original list is: {a} ")
print(f"List after multiplication is: {b}")


Output

Original list is: ['Geeks'] 
List after multiplication is: ['Geeks', 'Geeks', 'Geeks']

The time complexity of this code is O(n), where n is the number of copies of the original list. 

The auxiliary space complexity of this code is also O(n), where n is the number of copies of the original list. 

Example 4: Shorthand method for the same approach

Python3




# initializing a string with all True's
a = [True]*5
print(a)
 
# Initializing a list with all 0
a = [0]*10
print(a)


Output

[True, True, True, True, True]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

METHOD 3:Using for 

APPROACH:

The above code creates a new string by repeating the given string a specified number of times using a for loop.

ALGORITHM:

1.Initialize a variable string with the given string.
2.Initialize a variable n with the desired number of copies.
3.Initialize an empty string new_string.
4.Loop through n times using a for loop and for each iteration, concatenate the original string to the new_string.
5.Print the resulting new_string.

Python3




string = "Geeks"
n = 3
new_string = ""
for i in range(n):
    new_string += string
print(new_string)


Output

GeeksGeeksGeeks

Time Complexity:
The time complexity of the above code is O(n) because the for loop runs n times.

Space Complexity:
The space complexity of the above code is also O(n) because we are storing the resulting new_string in memory, which can grow up to n times the size of the original string.

RELATED ARTICLES

Most Popular

Recent Comments