Python – Alternate Character Addition


Introduction

Python Language comes under the OOPS concept and it runs the code immediately without checking for errors. Apart from the other languages it has got its unique place because of its advantages like it’s easy to code with simple syntax and statements. Strings are composed of characters and character addition is one of the string manipulation techniques used to alter the characters in the strings. Then append those altered characters together to form a new string.

Alternate Character Addition

Approach

Approach 1 βˆ’ Using the recursive function.

Approach 2 βˆ’ Using for loop.

Approach 3 βˆ’ Using join() method.

Approach 1: Python Program to Alternate Character Addition using recursive function

The Recursive function is used to take two input strings and return a string after alternating the character string.

Algorithm

  • Step 1 βˆ’ Two variables are initialized with string values.

  • Step 2 βˆ’ A function is defined with two parameters.

  • Step 3 βˆ’ if statement is used to check whether the strings are empty or not.

  • Step 4 βˆ’ The other condition is that the length of the string2 should be greater than string1.

  • Step 5 βˆ’ The return statement has join() and zip() functions along with the slicing of the elements.

  • Step 6 βˆ’ The first element S1[0] is added to S2[0] and S1[1] is added to S2[1], this process will continue until all the elements are added.

  • Step 7 βˆ’ The print statement will return the altered character.

Example

#initializing the two variables to store the string value
string1 = "Python"
string2 = "Golang"
#function is defined with two parameters of string data type
def add_alter(string_1: str, string_2: str) -> str:
   #check if two given strings are empty 
   if not (string_1 or string_2):
      return ""
   #checks whether the length of the string2 is greater than the string1    
   elif len(string_2) > len(string_1): # We make sure that the first variable holds a greater value than the second.
      temp_var = string_2
      string_2= string_1
   else:
      temp_var= string_2
   #using the join() and zip() function to alter the characters of the string 
   return ''.join([char[0] + char[1] for char in zip(string_1, string_2)]) + temp_var[len(string_2):]
#the function is called as it is a recursive function
new_str = add_alter("Python", "Golang")
#returns the new string after altering the characters
print("String alternate character addition:",new_str)

Output

String alternate character addition: PGyotlhaonng

Approach 2: Python Program to Alternate Character Addition using for loop

For loop will add each character to the empty string declared as β€œnew_str” along with its corresponding character from string1.

Algorithm

  • Step 1 βˆ’ Define two variables with string values.

  • Step 2 βˆ’ Use if statement to verify whether the strings are empty or not.

  • Step 3 βˆ’ The next condition would be deployed if string2’s length should be greater than string1.

  • Step 4 βˆ’ for loop is used to iterate through each character of the string.

  • Step 5 βˆ’ If the string is lengthy and the extra characters are stored in tem_var.

  • Step 6 βˆ’ The print statement will return the altered character.

Example

#initializing the two variables to store the string value
string1 = "Python"
string2 = "Golang"
#initializing the empty strings
new_str = ""
temp_var = ""
#check if two given strings are empty 
if not (string1 or string2):
   new_str = ""
   #checks whether the length of the string2 is greater than the string1    
elif len(string2) > len(string1):
   temp_var = string2
   string2 = string1
else:
   temp_var = string2
#for loop is used to iterate through the characters of the string
for i in range(len(string2)):
   new_str += string1[i] + string2[i]

new_str += temp_var[len(string2):]

#returns the new string after altering the characters
print("String alternate character addition:", new_str)

Output

String alternate character addition: PGyotlhaonng

Approach 3: Python Program to Alternate Character Addition using join() method

The zip() function is used to add the two given strings together and join() function appends all the elements of variables and zip() function using list comprehension.

Algorithm

  • Step 1 βˆ’ Creation of two variables containing string values.

  • Step 2 βˆ’ The return statement has join() and zip() function to add the characters of the string.

  • Step 3 βˆ’ If the length of the string2 is greater, the extra characters will be added at the end of new string.

  • Step 4 βˆ’ The print statement will return the altered character.

Example

#initializing the two variables to store the string value
string1 = "Python"
string2 = "Golang"
#using the join() and zip() function to alter the characters of the string 
new_str = ''.join([a + b for a, b in zip(string1, string2)]) + string2[len(string1):]
#returns the new string after altering the characters
print("String alternate character addition:", new_str)

Output

String alternate character addition: PGyotlhaonng

Conclusion

Python is a flexible and high-level language that is easily understood by the user. In the current world, managing data is the most challenging task for organizations with a high volume of data, and with the development of data science and machine learning it has become easier to access.

Updated on: 25-Aug-2023

101 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements