Removal of chars from a larger string and re-adding char but ensuring same invalid chars are not used

Viewed 33

I thought it would be a learning project to create a password generator. I continue to try to expand it as much as I can until I get stuck.

Im not looking for you to do it, only to help guide me in the most pythonic/elegant way of doing this as Im not entirely happy with my current solution (and who knows, it might just be the best answer)

Using tkinter I have a set of check buttons that allow for Caps, lowercase, digits, and symbols. Due to some fonts having characters that appear similar (l,I,0,O etc), I decided to put in a radio button that would remove any ambiguous characters.

    # Check if the radio button is set, else use individual checkboxes
if self.var_rbtn.get() == 2:
    if self.varUppercase.get() == 1:
        randChars += 'ABCDEFGHJKMNPQRSTUVWXYZ' #Removed LO
    if self.varLowercase.get() == 1:
        randChars += 'abcdefghijkmnopqrstuvwxyz' #Removed l
    if self.varNumbers.get() == 1:
        randChars += '123456789'  # Removed 0
    if self.varSymbols.get() == 1:
        randChars += r"""!"#$%&*=?@^"""  #Removed many

Again, the code above works, but Im not happy with it.

So the question is what is the best way to remove specific characters for a larger string, and replace them ensuring the same invalid chars are not readded...or is the solution previously provided sufficient?

1 Answers

As far as I have under stood you need to replace the unwanted characters from a string. You can try this :

import re
myStrting = "IlO0hasf$#gh8&^%(Ilo0|"
print(re.sub('[(#%$*%!@&()|^\\\/)(IOl0)]', '', myStrting))
Related