I have a a user-inputted polynomial and I only want to use it if it only has characters in the string 1234567890^-+x.
1234567890^-+x
How can I check if it does or not
Use a regular expression:
import re if re.match('^[-0-9^+x]*$', text): # Valid input
The re module comes with Python 2.5, and is your fastest option.
re
Demo:
>>> re.match('^[-0-9^+x]*$', '1x2^4-2') <_sre.SRE_Match object at 0x10f0b6780>