Open In App

Python | Append K character N times

Sometimes, we wish to manipulate a string in such a way in which we might need to add additional K at the end of string in case of filling the missing bits or any other specific requirement. The solution to this kind of problems is always handy and is good if one has knowledge of it. Let’s discuss certain ways in which this can be solved. 

Method #1 : Using ljust() This task can be performed using the simple inbuilt string function of ljust in which we just need to pass N times required and the element to right pad, in this case being K. 






# Python3 code to demonstrate
# Append K character N times
# using ljust()
 
# initializing string
test_string = 'GFG'
 
# printing original string
print("The original string : " + str(test_string))
 
# initializing K
K = 'M'
 
# No. of K required
N = 5
 
# using ljust()
# Append K character N times
res = test_string.ljust(N + len(test_string), K)
 
# print result
print("The string after adding trailing K : " + str(res))

Output : 
The original string : GFG
The string after adding trailing K : GFGMMMMM

Method #2 : Using format() String formatting using the format function can be used to perform this task easily, we just mention the number of elements total, element needed to pad, and direction of padding, in this case right. 






# Python3 code to demonstrate
# Append K character N times
# using format()
 
# initializing string
test_string = 'GFG'
 
# printing original string
print("The original string : " + str(test_string))
 
# initializing K
K = '0'
 
# No. of zeros required
N = 5
 
# using format()
# Append K character N times
temp = '{:<' + K + str(len(test_string) + N) + '}'
res = temp.format(test_string)
 
# print result
print("The string after adding trailing K : " + str(res))

Output : 
The original string : GFG
The string after adding trailing K : GFG00000

Method #3: Using +,* operators




# Python3 code to demonstrate
# Append K character N times
 
# initializing string
test_string = 'GFG'
 
# printing original string
print("The original string : " + str(test_string))
 
# initializing K
K = 'M'
 
# No. of K required
N = 5
 
# Append K character N times
test_string=test_string+K*N
 
# print result
print("The string after adding trailing K : " + str(test_string))

Output
The original string : GFG
The string after adding trailing K : GFGMMMMM

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #4: Using repeat()




import itertools
# initializing string
test_string = 'GFG'
 
# printing original string
print("The original string : " + str(test_string))
 
# initializing K
K = 'M'
 
# No. of K required
N = 5
 
# using repeat()
# Append K character N times
test_string += "".join(itertools.repeat(K,N))
 
# print result
print("The string after adding trailing K : " + str(test_string))
#This code is contributed by Edula Vinay Kumar Reddy

Output
The original string : GFG
The string after adding trailing K : GFGMMMMM

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 5: Using a loop and concatenation




test_string = "GFG"
K = "M"
N = 5
 
for i in range(N):
    test_string += K
 
print("The original string: " + str(test_string))
 
print("The string after adding trailing K: " + str(test_string))

Output
The original string: GFGMMMMM
The string after adding trailing K: GFGMMMMM

Time complexity: O(N) because the loop runs N times. 
Auxiliary space: O(1) because only a few variables are used in addition to the input string.


Article Tags :