
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Alternate Character Addition in Python
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.