Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get all chars not in string python

Tags:

python

I'm trying to randomly insert characters into a string and I want to be able to strip them out later, so I have to use characters that are not already in the string. I want to use as many characters as possible. How can I get a list of all the characters that are not in the string? I am using Python 2.

like image 211
88888 Avatar asked Jan 30 '26 12:01

88888


2 Answers

Assuming you have a set of all possible characters:

>>> characters = set('ABCabc')

then it is as simple as

>>> my_str = "abbaAC"
>>> not_in_string = characters - set(my_str)
>>> not_in_string
set(['c', 'B'])
like image 53
chepner Avatar answered Feb 01 '26 03:02

chepner


The big assumption I'm making here is that you're working with an ASCII string.

Valid characters have integer values between 0 and 255. As such, the following will generate a complete set of all of the valid characters:

all_chars = set(chr(i) for i in range(256))

You can then get the set of characters in your string. That's as easy as running set(mystring).

The difference between those is the subset of what's in all_chars, but not in your string:

unused_chars = all_chars - set(mystring)

So putting that all together:

def get_unused_chars(mystring):
    # Generate the list of every valid ASCII character
    all_chars = set(chr(i) for i in range(256))

    # Determine which characters are unused
    unused_chars = all_chars - set(mystring)

    return unused_chars
like image 32
Moshe Avatar answered Feb 01 '26 01:02

Moshe



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!