samedi 27 février 2021

How do I remove multiple random characters from a string in python

I have come up with the following code but unfortunately its only removing 1 character from my string.

import random
string = 'HelloWorld!'
def remove_random_character(phrase):
        character_number = random.randint(0, len(phrase))
        remover = f'{phrase[:character_number - 1]}_{phrase[character_number:]}'
        for _ in range(8):
            sliced_phrase = remover
        print(sliced_phrase)
remove_random_character(string)```

 I thought that the for loop will take care of this but unfortunately it did not. but every time it loops it just refreshes the *sliced_phrase* variable. but I do not know how to store the last version of the loop, for that to be edited. So how can I can I remove multiple random characters from a string?



Aucun commentaire:

Enregistrer un commentaire