samedi 7 décembre 2019

Replace all elements in a string except N, n and spaces in python

I want to replace all elements in a string with a # except for N and n. This is the code that i have been working

test_str = ("BaNana")
for x in test_str:
    if x != "n" or x !="N":
        ari = test_str.replace(x, "#")
        print(ari)

The output that i get is

    #aNana
    B#N#n#
    Ba#ana
    B#N#n#
    B#N#n#

Where as the output that i want is

    ##N#n#

Aucun commentaire:

Enregistrer un commentaire