Tuesday, December 3, 2024
Google search engine
HomeLanguagesPython | Alternate character addition

Python | Alternate character addition

Sometimes, while working with Python, we can have a problem in which we need to add a character after every character in String. This kind of problem can have its application in many day-day programming domains. Lets discuss certain ways in which this task can be performed.

Method #1 : Using loop 

This is brute force way in which this task can be performed. In this, we iterate for each element and insert the character required. We erase the last stray character. 

Python3




# Python3 code to demonstrate working of
# Alternate character addition
# Using loop
 
# initializing string
test_str = "neveropen"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing K
K = '*'
 
# Alternate character addition
# Using loop
res = ''
for ele in test_str:
    res += ele + K
res = res[:-1]
 
# printing result
print("String after character addition : " + str(res))


Output : 

The original string is : neveropen
String after character addition : g*e*e*k*s*f*o*r*g*e*e*k*s

 

 
Method #2 : Using join() 
This is easiest, elegant and recommended way to perform this task. In this, we just use one line to perform this task. The join() is used to perform it.
 

Python3




# Python3 code to demonstrate working of
# Alternate character addition
# Using join()
 
# initializing string
test_str = "neveropen"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing K
K = '*'
 
# Alternate character addition
# Using join()
res = K.join(test_str)
 
# printing result
print("String after character addition : " + str(res))


Output : 

The original string is : neveropen
String after character addition : g*e*e*k*s*f*o*r*g*e*e*k*s

 

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

Method 3: Using List Comprehension

This method uses a list comprehension to build a list of strings with the added character, and then joins the list into a final string. The list comprehension creates a list of n strings, where each string is a character from the input string concatenated with the added character. This list is then joined into a final string using the ”.join() method. The time complexity is O(n) since we are performing n string concatenations and n element additions to the list. The space complexity is also O(n) since we are creating a list of n strings.

Python3




def add_char(test_str, char):
  return ''.join([s + char for s in test_str])
 
test_str = "neveropen"
char = '*'
 
res = add_char(test_str, char)
print("String after character addition : " + str(res))


Output

String after character addition : g*e*e*k*s*f*o*r*g*e*e*k*s*

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

Method 4: Using Recursion

In this approach, we define a recursive function alt_addition that takes in the string s, alternate character k, and a starting index i (which is set to 0 by default).

Algorithm: 

  1. The function concatenates the current character at index i with the alternate character k.
  2. Then recursively calls itself with the same string s, alternate character k, and the next index i+1 until it reaches the end.
  3. The base case is when i == index of the last character, and in this case, the function simply returns the last character.
  4. We then initialize the input string test_str and the alternate character K, and call the alt_addition function with these parameters to obtain the result. Finally, we print the resulting string.

Below is the code for the above approach:

Python3




# Python code to demonstrate working of
# Alternate character addition
# Using recursion
 
# Function to perform alternate character addition recursively
 
 
def alt_addition(s, k, i=0):
    if i == len(s) - 1:
        return s[i]
    return s[i] + k + alt_addition(s, k, i+1)
 
 
# Initializing string
test_str = "neveropen"
 
# Printing original string
print("The original string is : " + test_str)
 
# Initializing K
K = '*'
 
# Alternate character addition
# Using recursion
res = alt_addition(test_str, K)
 
# Printing result
print("String after character addition : " + str(res))


Output

The original string is : neveropen
String after character addition : g*e*e*k*s*f*o*r*g*e*e*k*s

Time Complexity: O(n), where n is the length of the input string
Auxiliary Space: O(n), because each recursive call to alt_addition adds a new stack frame to the call stack

Method 4: Use a generator function along with the join() method.

Approach:

  1. Define a generator function generate_chars() that takes two arguments – the input string s and the character k to be added alternatively.
  2. Initialize a variable i to 0.
  3. While i is less than the length of the input string s, yield the character at the current index i, followed by the character k.
  4. Increment i by 1.
  5. Use the join() method to join the characters returned by the generator function.
  6. Return the resulting string.

Python3




# Python code to demonstrate working of
# Alternate character addition
# Using generator function and join()
 
# Generator function to yield characters of input string and k alternatively
 
 
def generate_chars(s, k):
    i = 0
    while i < len(s):
        yield s[i]
        yield k
        i += 1
 
 
# initializing string
test_str = "neveropen"
 
# printing original string
print("The original string is : " + test_str)
 
# initializing K
K = '*'
 
# Alternate character addition using generator function and join()
res = ''.join(generate_chars(test_str, K))
 
# printing result
print("String after character addition : " + str(res))


Output

The original string is : neveropen
String after character addition : g*e*e*k*s*f*o*r*g*e*e*k*s*

Time complexity: O(n)
Auxiliary space: O(n)

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments