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

powershell Start-Process exit code -1073741502 when used with Credential from a windows service environment -

twig - Using Twigbridge in a Laravel 5.1 Package -

c# - LINQ join Entities from HashSet's, Join vs Dictionary vs HashSet performance -