Python – Append K characters N times


Introduction

In this article, the user will focus on how to Append K characters N times in python. In the Python Language, append() function is used to add a character to the existing list, but to add K characters in N number of times different approach is utilized. The first approach utilize the join() method, the second approach utilize the textwrap() method and the last approach utilize the reduce() method to add the character to the existing string.

Append K characters N times

The methods to do this can be varying and some may be simple loop iteration on the other side it can be some of the functions and methods.

Approach

Approach 1 βˆ’ Using join() method

Approach 2 βˆ’ Using textwrap() method

Approach 3 βˆ’ Using reduce() method

Approach 1: Python Program to append K characters N times using join() function

The number of times, the character of a string that needs to be repeated is initialized as six. The character that is repeated six times is initialized as β€œ$”. The for loop is used to iterate through the string and the main string is β€œpython” to which the character is added using the join method.

Algorithm

  • Step 1 βˆ’ The character is initialized as β€œPython” and the number of times that has been added is given as N=6.

  • Step 2 βˆ’ Then finally generated list is appended using the join() function to the single string β€˜$’.

  • Step 3 βˆ’ Then to get the desired output, the string β€˜python’ is used and added to the empty separator.

  • Step 4 βˆ’ Then the appended statement is printed according to the N value.

Example

#the number of times the character has to be repeated
N = 6
#the character that has to be repeated
K_character = '$'
#adding the character to the existing string using join method
test_list = [K_character for i in range(N)]
num_string = 'Python'+''.join(test_list)

#Returning the print statement with the K character of N=6 times 
print(num_string)

Output

Python$$$$$$

Approach 2: Python Program to append K characters N times using textwrap() method

The number of times, the character of a string that needs to be repeated is initialized as six. The character that is repeated six times is initialized as β€œ$”. The textwrap() method is used for adding the character to the existing string.

Algorithm

  • Step 1 βˆ’ The textwrap module is imported into the Python program.

  • Step 2 βˆ’ The character is initialized as β€œPython” and the number of times that has been added is given as N=6.

  • Step 3 βˆ’ The textwrap module is used to wrap the character β€˜$’ with the string β€œPython” of the given length using the join() method to add to the empty separator.

  • Step 4 βˆ’ Then the appended statement is printed according to the N value.

Example

#importing the textwrap module
import textwrap

#the number of times the character has to be repeated
N = 6
#the character that has to be repeated
K_character = '$'

#the character needs to be added to the existing string
num_string = 'Python'

#adding the character to the existing string using textwrap method
wrap_charc = textwrap.wrap(num_string)
complete_str = ''.join(wrap_charc) + K_character * (N)

#Returning the print statement with the K character of N=6 times 
print(complete_str)

Output

Python$$$$$$

Approach 3: Python Program to append K characters N times using reduce() method

The character is initialized as β€œPython” and the number of times that has been added is given as N=6. Then the appended statement is printed according to the N value. The reduce() method is used for adding the character to the existing string.

Algorithm

  • Step 1 βˆ’ The character is initialized as β€œPython” and the number of times that has been added is given as N=6.

  • Step 2 βˆ’ The functools module is used to add the character β€˜$’ with the string β€œPython” of the given length using the reduce() method.

  • Step 3 βˆ’ Then the appended statement is printed according to the N value.

Example

#importing the functools module
from functools import reduce

#the number of times the character has to be repeated
N = 6
#the character that has to be repeated
K_character = '$'

#the character needs to be added to the existing string
num_string = 'Python'

#adding the character to the existing string using reduce method
complete_str = reduce(lambda a, b: a + K_character, range(N), num_string)

#Returning the print statement with the K character of N=6 times 
print(complete_str)

Output

Python$$$$$$

Conclusion

Python has got a remarkable place in the field of handling data with the technologies like data science and machine learning. Python is popular worldwide because of its simplicity and flexibility with other applications. The ways in which the K characters can be added according to the number of times are done using various methods.

Updated on: 25-Aug-2023

132 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements