2

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.

3
  • 1
    There are a lot of characters which are not in the string. Do you really want all of them? Commented Aug 24, 2015 at 20:12
  • use sets. (e.g. : set('some letters') - set('other letters')) Commented Aug 24, 2015 at 20:12
  • 2
    ASCII characters? Use string.ascii_letters and set. Commented Aug 24, 2015 at 20:13

5 Answers 5

5

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'])
Sign up to request clarification or add additional context in comments.

Comments

2

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

2 Comments

Do you know how I could only use characters that don't start with a backslash?
use string.printable (from docs.python.org/2/library/string.html#string.printable ). You can also use other characters sets if you don't want to include the whitespace characters.
2

What is a "letter"?

Assuming ascii:

set(string.ascii_letters) - set(yourstring)

otherwise, define your alphabet appropriately first, then use

set(youralphabet) - set(yourstring)

Comments

0

You can use a list comprehension to achieve this:

>>> not_in = 'abcde'                                                                                                       
>>> other_string = 'remove char in'                                                                                        

>>> char_not_in = "".join([c for c in other_string if not c in not_in])                                                    
>>> print(char_not_in)
'rmov hr in'

Comments

0

The following should do the trick (given that you want your output string to have a specific size):

from string import letters
from random import choice


def random_letters(not_in, size=1):
    i = 1
    out = []
    while(i <= size):
        char = choice(letters)
        if char.lower() not in not_in:
            out.append(char)
            i += 1
    return ''.join(c for c in out)

print(random_letters('abc', 3))

this outputs a random string without a, b or c of size 3.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.