how can I check i only have l/r/u/d/x/y/z/w in a string? (python)
I have a string
I want to check it contains only l/r/u/d/x/y/z/w
but I can't think of a short code to do so
Glad for your help
Just use sets and check if the set of letters in your sentence is a subset of all your letters:
Note that the argument to
subsetdoesn't have to be a set, any iterable is accepted.
A bit clearer than Thierry's answer:
all(char in "lrudxyzw" for char in mystring)
import re string="xyz" if re.match('^[lrudxyzw]*$', string): do code
Could also use
if re.fullmatch('[lrudxyzw]*', string): do code
That checks whether stripping the good letters doesn't leave anything behind.