pythonpython-3.xlistcharacter-replacement

Python3: change one character in a list of characters


I want to read a string from stdin and append this string to a list. Then I want to change one character at a time and append the new strings to the same list.

pat=sys.argv[1]
dummy1=list(str(pat))

myList=[]
list.append(pat)
c=0
for letter in dummy1:
    if dummy1[c]=="A":
        dummy1[c]=="C"
        depp=''.join(dummy1)
        myList.append(depp)
    c+=1
print(myList)

but when I try this, I only get a list containing my original string as often as I looped. What did I miss?


Solution

  • Try This ( in line 8 you should replace == with = ):

    pat = "SALAM"   # Your Input Data
    
    dummy1 = list(str(pat))
    my_list = [pat]
    
    c = 0
    for letter in dummy1:
    
        if dummy1[c] == "A":
    
            dummy1[c] = "C"
            join_list = ''.join(dummy1)
            my_list.append(join_list)
    
        c += 1
    
    last_data = my_list[len(my_list)-1]
    print(last_data)