Replace letters in a string using Python -
this question has answer here:
i'm new python , i'm trying understand basic thing. have code:
def mix_up(a, b): a,b=b[0:2]+a[2:], a[0:3]+b[3:] print (a,b) mix_up("abcd","efgh")
why b doesn't "new" 3 letters of (i.e, "efch")? there elegant of doing in 1 line, or have use other variables?
thanks!
the reason not working because assignment (the left hand side of =
) happens after entire right hand side evaluated.
so a
, b
still "old" a
, b
in statement, , not new ones.
to fix it, split 2 statements:
a = b[0:2]+a[2:] b = a[0:3]+b[3:] # "new"
Comments
Post a Comment