PYTHON PROGRAM THAT ASKS THE USER TO ENTER TWO STRINGS OF SAME LENGTH AND WILL ALTERNATE THE CHARACTERS OF THE TWO STRINGS.FOR EXAMPLE: abcde and ABCDE THEN THE PROGRAM WILL PRINT AaBbCcDdEe
Write a program that asks the user to enter two strings of the same length. The program should then check to see if the strings are of the same length. If they are not, the program should print an appropriate message and exit. If they are of the same length, the program should alternate the characters of the two strings. For example, if the user enters abcde and ABCDE the program should print out AaBbCcDdEe.
CODE:
def alternate_string(s1,s2):
new_string=""
if len(s1)==len(s2):
print("The two strings are of same length")
for i in range(len(s1)):
new_string=new_string+s2[i]+s1[i]
else:
print("The two strings are not of same length")
return new_string
string1=input("Enter string-1:")
string2=input("Enter string-2:")
print("New string is:",alternate_string(string1,string2))
EXPLANATION:
In this program we have defined a function named alternate_string which accepts two strings as parameters.
In this function, we have created an empty string(new_string="").And using len() function we check the length of two strings.
If they are equal then, it will add the characters of the two strings alternately to the new_string using for loop.
The function will return the new_string.
If the length of two strings is not equal then the program will print an appropriate message.
Next we have to call the function by passing two strings as parameters.
OUTPUT:
Comments
Post a Comment