python-tutorial-Python-courses-python-exercises

Exercise 48

Write a program as a function which takes as parameters a pair of strings (s1, s2) and which returns the string s obtained by concatenating in an alternative way the characters of s1 and s2. Example for (s1, s2) = ("Python" , "Java") the function returns the string s = 'PJyatvha'

Solution

# creating a function which test if given word contains at least one uppercase character
def concatenation(s1 , s2):
# initializing the concatenated string s
s = ""
n = len(s1)

m = len(s2)
if n < m :
for i in range(0 , n):
s = s + s1[i] + s2[i]
s = s + s2[n+1 : m]
else:
for i in range(0 , m):
s = s + s1[i] + s2[i]
s = s + s2[m+1 : n]

return s
print("The concatenated string is s = " ,concatenation("Python" , "Java"))

# The output is : The concatenated string is s = PJyatvha

Younes Derfoufi
my-courses.net

Leave a Reply