Open In App

Python | Add leading K character

Last Updated : 15 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Sometimes, during the string manipulation, we are into a problem where we need to pad or add leading K to the string as per the requirements. This problem can occur in web development. Having shorthands to solve this problem turns to be handy in many situations. Let’s discuss certain ways in which this problem can be solved.

Method #1 : Using rjust() 
rjust function offers a single line way to perform this particular task. Hence can easily be employed to any string whose padding we need to be done. We can specify the amount of padding required.

Python3




# Python3 code to demonstrate
# Add leading K character
# using rjust()
 
# initializing string
test_string = 'GFG'
 
# printing original string
print("The original string : " + str(test_string))
 
# No. of zeros required
N = 4
 
# initializing K
K = 'M'
 
# using rjust()
# Add leading K character
res = test_string.rjust(N + len(test_string), K)
 
# print result
print("The string after adding leading K : " + str(res))


Output : 

The original string : GFG
The string after adding leading K : MMMMGFG

 

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

Python3




# Python3 code to demonstrate
# Add leading K character
# using format()
 
# initializing string
test_string = 'GFG'
 
# printing original string
print("The original string : " + str(test_string))
 
# No. of zeros required
N = 4
 
# initializing K
K = '0'
 
# using format()
# Add leading K character
# N for number of elements and '>' for leading
temp = '{:>' + K + '7}'
res = temp.format(test_string)
 
# print result
print("The string after adding leading K : " + str(res))


Output : 

The original string : GFG
The string after adding leading K : 0000GFG

 

Method #3 : Using replace() method 

Python3




# Python3 code to demonstrate
# Add leading K character
 
# initializing string
test_string = 'GFG'
 
# printing original string
print("The original string : " + str(test_string))
 
# No. of zeros required
N = 4
 
# initializing K
K = 'M'
 
# Add leading K character
test_string=test_string.replace(test_string[0],K*N+test_string[0],1)
 
# print result
print("The string after adding leading K : " + str(test_string))


Output

The original string : GFG
The string after adding leading K : MMMMGFG

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

Method #4: Using repeat() method 

It can be used to repeatedly iterate over an object, such as a string. Here is an example of how it can be used to add leading K characters to a string:

Python




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


Output

The original string : GFG
The string after adding leading K : MMMMGFG

This approach uses the repeat() function from the itertools module to create a new string consisting of N repetitions of the character K. Then it concatenates this new string with the original string to add the leading K characters.

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

Method #5: Using string concatenation with a loop

  • Initialize an empty string variable, let’s call it “res”.
  • Determine the number of leading characters required, N.
  • Determine the character that will be used for the leading characters, K.
  • Use a for loop to concatenate K to res N times.
  • Concatenate the original string, test_string, to res.
  • Print the result, res

Python3




# initializing string
test_string = 'GFG'
 
# printing original string
print("The original string : " + str(test_string))
 
# No. of leading K required
N = 4
 
# initializing K
K = 'M'
 
# using string concatenation with a loop
# Add leading K character
res = ""
for i in range(N):
    res += K
res += test_string
 
# print result
print("The string after adding leading K : " + str(res))


Output

The original string : GFG
The string after adding leading K : MMMMGFG

Time complexity: O(N), where N is the number of leading characters required.
Auxiliary space: O(N), since we need to store the N leading characters in memory.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads