python - Replacing Characters in String: first one character replaces with string then another character with another string -


if start string called x n=0 , is

x="g" 

then if have range n , 1 , want replace g x srgrs, that

x1="srgrs" 

then if n=2, want replace s in x1 glslg , g srgrs, get

x2="glslgrsrgrsrglslg" 

and continue n+=1, i'm replacing "g" , "s" x2 corresponding string..

how write loop, continuously?

i've tried use str.replace() , can't work :(

edit: in loop: if there "g"in string, should replaced "srgrs" if there "s" in string, should replaced "glslg"

for nbiter iterations, replaces simultaneously "s" "glslg" , "g" "srgrs".

nbiter=4 x="g" print(x)  in range(nbiter):     newx=""     char in x:         if char=="s":             newx+="glslg"         elif char=="g":             newx+="srgrs"         else:             newx+=char     x=newx     print(x) 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -

jdbc - Not able to establish database connection in eclipse -