Python | Repeat String till K
Last Updated :
21 Apr, 2023
Sometimes, while working with strings, we might encounter a use case in which we need to repeat our string to the size of K, even though the last string might not be complete, but has to stop as the size of string becomes K. The problem of repeating string K times, is comparatively simpler than this problem. Let’s discuss way outs we can perform to solve this problem.
Method #1 : Using list slicing and // operator
This task can be performed using the above tools. In this we just multiply the string till it becomes greater than or equal to K, and then just omit the slice of extra string using the list slicing method.
Python3
test_string = "GeeksforGeeks"
K = 30
print ( "The original string : " + str (test_string))
res = (test_string * (K / / len (test_string) + 1 ))[:K]
print ( "String after performing repetition : " + res)
|
Output :
The original string : GeeksforGeeks
String after performing repetition : GeeksforGeeksGeeksforGeeksGeek
Time complexity: O(n)
Auxiliary space: O(n)
Method #2 : Using divmod() + list slicing
The division applied in the above method can be substituted in this method with the divmod function, which improves code readability with the cost of 40% of performance degradation.
Python3
test_string = "GeeksforGeeks"
K = 30
print ( "The original string : " + str (test_string))
div, mod = divmod (K, len (test_string))
res = test_string * div + test_string[:mod]
print ( "String after performing repetition : " + res)
|
Output :
The original string : GeeksforGeeks
String after performing repetition : GeeksforGeeksGeeksforGeeksGeek
Time complexity: O(1) since the operations are not dependent on the size of the input.
Auxiliary space: O(len(test_string)) since we create a string of length len(test_string) to store the repeated string.
Method #3: Using while loop and slicing
Python3
test_string = "GeeksforGeeks"
K = 30
print ( "The original string : " + str (test_string))
res = ""
while ( len (res) < = K):
res + = test_string
print ( "String after performing repetition : " + res[:K])
|
Output
The original string : GeeksforGeeks
String after performing repetition : GeeksforGeeksGeeksforGeeksGeek
Time complexity: O(K), where K is the maximum length of the repeated string.
Auxiliary space: O(K), as we are creating a new string ‘res’ to store the repeated string, and its maximum length can be K.
Method 4 : use the math module to determine the number of times the string needs to be repeated to reach the desired length, and then concatenate the string accordingly.
step-by-step approach:
Import the math module.
Initialize the original string and the desired length K.
Calculate the length of the original string using the len() function.
Calculate the number of times the original string needs to be repeated to reach the desired length using the math.ceil() function, which rounds up the result to the nearest integer.
Concatenate the original string the required number of times using string multiplication.
Trim the concatenated string to the desired length K using list slicing.
Print the final string.
Python3
import math
test_string = "GeeksforGeeks"
K = 30
length = len (test_string)
repetitions = math.ceil(K / length)
res = test_string * repetitions
res = res[:K]
print ( "String after performing repetition : " + res)
|
Output
String after performing repetition : GeeksforGeeksGeeksforGeeksGeek
Time complexity: The time complexity of this approach is O(K), where K is the maximum length of the repeated string.
Auxiliary space: The auxiliary space used by this approach is also O(K),
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...