Reputation: 433
I am beginner in python and i am working on a small task to join two string character by character without using any predefined function especially when string are not same in length
for ex: s1 = 'MICROSOFT', s2 = 'CORPS' then output will be >> MC IO CR RP OS SOFT
I have written below code
s1 = 'MICROSOFT'
s2 = 'CORPS'
for i in range(len(s1) and len(s2)):
if len(s1)==len(s2):
var = s1[i] + s2[i]
print(var , end='')
elif len(s1)!=len(s2):
if len(s1)>len(s2):
var1 = s1[i] + s2[i]
print(var1, end=' ')
By using above code i have achieved output like this: MC IO CR RP OS
How i can print the last part i.e SOFT??
Upvotes: 2
Views: 54
Reputation: 659
You could do something like this after your loop
s1 = 'MICROSOFT'
s2 = 'CORPS'
for i in range(len(s1) and len(s2)):
if len(s1)==len(s2):
var = s1[i] + s2[i]
print(var , end='')
elif len(s1)!=len(s2):
if len(s1)>len(s2):
var1 = s1[i] + s2[i]
print(var1, end=' ')
if len(s1) > len(s2):
print(s1[len(s2):])
elif len(s2) > len(s1):
print(s2[len(s1):])
Upvotes: 2