Python | Append K character N times
Last Updated :
15 May, 2023
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
# 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
# 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
# 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))
OutputThe original string : GFG
The string after adding trailing K : GFGMMMMM
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #4: Using repeat()
Python3
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
OutputThe 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
- Initialize a variable named test_string with the string value "GFG".
- Initialize two variables, K with the string value "M" and N with the integer value 5.
- Start a for loop that will iterate N times, with the loop variable i taking on values from 0 to 4.
- Inside the loop, append the string value of K to test_string using the += operator. This will add K to the end of test_string each time the loop iterates.
- After the loop, print the string "The original string: " concatenated with the string representation of test_string using the str() function.
- Print the string "The string after adding trailing K: " concatenated with the string representation of test_string using the str() function. This will display the same value as the previous print statement since the test_string variable has not been modified since the loop.
Python3
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))
OutputThe 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.
Similar Reads
Python - Remove N characters after K Given a String, remove N characters after K character. Input : test_str = 'ge@987eksfor@123geeks is best@212 for cs', N = 3, K = '@' Output : 'geeksforgeeks is best for cs' Explanation : All 3 required occurrences removed. Input : test_str = 'geeksfor@123geeks is best for cs', N = 3, K = '@' Output
2 min read
Python - Append according to Kth character Given a String list, append to String i or j value depending on Kth index value. Input : test_list = ["geeksforgeeks", "best", "for", "geeks"], K = 2, N = 'e', i, j = "@@", ".." Output : ['geeksforgeeks..', 'best@@', 'for@@', 'geeks..'] Explanation : geeksforgeeks and geeks having similar 2nd occ. v
3 min read
Python | Add leading K character 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 t
4 min read
Get Last N characters of a string - Python We are given a string and our task is to extract the last N characters from it. For example, if we have a string s = "geeks" and n = 2, then the output will be "ks". Let's explore the most efficient methods to achieve this in Python.Using String Slicing String slicing is the fastest and most straigh
2 min read
Python | Replace characters after K occurrences Sometimes, while working with Python strings, we can have a problem in which we need to perform replace of characters after certain repetitions of characters. This can have applications in many domains including day-day and competitive programming. Method #1: Using loop + string slicing This is brut
5 min read
Python - Reverse Shift characters by K Given a String, reverse shift each character according to its alphabetic position by K, including cyclic shift. Input : test_str = 'bccd', K = 1 Output : abbc Explanation : 1 alphabet before b is 'a' and so on. Input : test_str = 'bccd', K = 2 Output : zaab Explanation : 2 alphabets before b is 'z'
3 min read