Encode.to.code
Encode.to.code

Reputation: 99

How to add two strings by index

I tried many times but could not do it.

Here is an example:

print( concat_corresponding( "12345", "6789XYZ" ) )

Desired output:

162738495XYZ

Upvotes: 3

Views: 115

Answers (6)

Jim Dennis
Jim Dennis

Reputation: 17500

from itertools import izip_longest
''.join(['%s%s' % (x ,y)\
         for x,y in izip_longest("12345","6789XYZ", fillvalue='')])

## Was: ''.join(['%s%s' % (x if x else '',y if y else '') \
##         for x,y in izip_longest("12345","6789XYZ")])

To break that down a bit:

  • the builtin zip() function zips only to the shorter of the two sequences; so we use izip_longest() from the itertools standard library module
  • izip_longest() pads the generated sequences with None by default; so we add the fillvalue='' optional (keyword) argument
  • the resulting substrings are all just joined together to form your results.

Upvotes: 2

jpp
jpp

Reputation: 164643

This is one way with itertools:

from itertools import chain, zip_longest

a = "12345"
b = "6789XYZ"

res = ''.join(list(chain.from_iterable(zip_longest(a, b, fillvalue=''))))

# '162738495XYZ'

Note the list conversion is not required, but improves performance.

Upvotes: 3

Vasilis G.
Vasilis G.

Reputation: 7846

Another way of doing it is the following:

def concat_corresponding(string1, string2):
    minimum, maximum = sorted([string1, string2], key=len)
    return "".join(string1[i]+string2[i] for i in range(len(minimum))) + maximum[len(minimum):]

print(concat_corresponding( "12345", "6789XYZ" )) 

Output:

162738495XYZ   

Upvotes: 0

Patrick Artner
Patrick Artner

Reputation: 51643

You can levarege list comprehensions, join and zip:

te1 = "12345"
te2 = "6789XYZ"

print (''.join( ''.join(x) for x in zip(te1,te2)) + (te1[len(te2):] if len(te1)>len(te2) else te2[len(te1):]))
                                                    # ^^^^ this part adds the remainer of longer list to result

output:

162738495XYZ

https://docs.python.org/3/library/functions.html#zip

https://docs.python.org/3/library/stdtypes.html#str.join

Explanation:

zip pairs up the characters by position, the rest makes the list of pairs back into a string.

Zip only works for the shorter length string - you can switch to itertools.zip_longest (see JimDennis answer) or append the longer lists part (like I did here)

itertools.zip_longest(*iterables, fillvalue=None)

Upvotes: 2

A.Raouf
A.Raouf

Reputation: 2320

This one is basic but useful

str1="12345"
str2="6789XYZ"
str3=""
i=0
for i, ch in enumerate(zip(str1, str2)):
    str3 += ch[0] + ch[1]

if len(str1) < len(str2):
    str3 += str2[i+1:]
else:
    str3 += str1[i+1:]
print(str3)

Upvotes: 0

bphi
bphi

Reputation: 3185

I think this solution is a little cleaner and takes advantage of the fillvalue keyword argument for itertools.zip_longest.

from itertools import zip_longest 

''.join(a+b for a, b in zip_longest(s1, s2, fillvalue=''))

Upvotes: 1

Related Questions