Python | Add leading K character
Last Updated :
15 May, 2023
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
test_string = 'GFG'
print ( "The original string : " + str (test_string))
N = 4
K = 'M'
res = test_string.rjust(N + len (test_string), K)
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
test_string = 'GFG'
print ( "The original string : " + str (test_string))
N = 4
K = '0'
temp = '{:>' + K + '7}'
res = temp. format (test_string)
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
test_string = 'GFG'
print ( "The original string : " + str (test_string))
N = 4
K = 'M'
test_string = test_string.replace(test_string[ 0 ],K * N + test_string[ 0 ], 1 )
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
test_string = 'GFG'
print ( "The original string : " + str (test_string))
N = 4
K = 'M'
res = ''.join(itertools.repeat(K, N)) + test_string
print ( "The string after adding leading K : " + str (res))
|
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
test_string = 'GFG'
print ( "The original string : " + str (test_string))
N = 4
K = 'M'
res = ""
for i in range (N):
res + = K
res + = test_string
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
Please Login to comment...