Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python – Insert character in each duplicate string after every K elements
When it is required to insert a character in each duplicate string after every K elements, a method is defined that uses the append method, concatenation operator and list slicing to create multiple versions of the string with the character inserted at different positions.
Example
Below is a demonstration of the same −
def insert_char_after_key_elem(my_string, my_key, my_char):
my_result = []
for index in range(0, len(my_string), my_key):
my_result.append(my_string[:index] + my_char + my_string[index:])
return my_result
my_string = 'PythonToCode'
print("The string is :")
print(my_string)
K = 2
print("The value of K is")
print(K)
add_char = ";"
print("The result is :")
result = insert_char_after_key_elem(my_string, K, add_char)
for item in result:
print(item)
Output
The string is : PythonToCode The value of K is 2 The result is : ;PythonToCode Py;thonToCode Pyth;onToCode Python;ToCode PythonTo;Code PythonToCo;de
How It Works
The function works by creating multiple copies of the original string, each with the specified character inserted at different positions. The positions are determined by multiples of K (0, K, 2K, 3K, etc.). For each position, it uses string slicing to split the original string and insert the character at that position.
Alternative Implementation
Here's a more readable version that prints each result separately −
def insert_char_variations(text, step, char):
variations = []
for i in range(0, len(text) + 1, step):
new_string = text[:i] + char + text[i:]
variations.append(new_string)
return variations
text = "Hello"
variations = insert_char_variations(text, 2, "*")
print(f"Original string: {text}")
print("Variations with '*' inserted every 2 positions:")
for i, variation in enumerate(variations):
print(f"Position {i*2}: {variation}")
Original string: Hello Variations with '*' inserted every 2 positions: Position 0: *Hello Position 2: He*llo Position 4: Hell*o
Explanation
A method named
insert_char_after_key_elemis defined that takes a string, a key (step size) and a character as parameters.An empty list is defined to store the result variations.
The function iterates through positions at intervals of K (0, K, 2K, etc.) using
range(0, len(my_string), my_key).List slicing and concatenation operator
+are used to insert the character at each position and append the result to the list.The method returns the list of string variations.
Outside the method, a string is defined and displayed on the console.
The K value and the character to insert are defined.
The method is called with the required parameters and results are displayed.
Conclusion
This technique creates multiple string variations by inserting a character at regular intervals. It's useful for generating test data or creating formatted strings with separators at specific positions.
