Most pythonic way to interleave two strings

前端 未结 14 1105
暗喜
暗喜 2020-11-27 14:34

What\'s the most pythonic way to mesh two strings together?

For example:

Input:

u = \'ABCDEFGHIJKLMNOPQRSTUVWXYZ\'
l = \'abcdefghijklmnopqrst         


        
14条回答
  •  旧巷少年郎
    2020-11-27 15:08

    You could also do this using map and operator.add:

    from operator import add
    
    u = 'AAAAA'
    l = 'aaaaa'
    
    s = "".join(map(add, u, l))
    

    Output:

    'AaAaAaAaAa'
    

    What map does is it takes every element from the first iterable u and the first elements from the second iterable l and applies the function supplied as the first argument add. Then join just joins them.

提交回复
热议问题