How to mix strings in Python -
i trying write function mix strings in python getting stuck @ end. example, have 2 words, mix , pod. create function returns: pox mid
my code returns pox mix
code:
def mix_up(a, b): if len(a , b)>1: b=str.replace(b,b[2],a[2:3]) a=str.replace(a,a[2],b[2]) print b,"",a return mix_up('mix','pod')
i seeking multiple words. example:
if used dog,dinner
the output should return dig donner
thanks!
little play on string slicing
def mix_up(first, second): new_first = second[:2] + first[2:] new_second = first[:2] + second[2:] return " ".join((new_first, new_second)) assert mix_up('mix','pod') == 'pox mid' assert mix_up('dog','dinner') == 'dig donner'
Comments
Post a Comment