When you have used the not
operator in Python, you probably have noticed that this code:
if "@" not in email_address: print("Error!")
outputs the same result as this code:
if not "@" in email_address: print("Error!")
Both expressions are functionally identical and produce the same output. In the Python documentation, you find an example how to compare a variable to a singleton. PEP8 says: "Use is not
operator rather than not ... is
".
When the same rule applies to in
, this is the correct way to check if a character is not in a string:
email_address = "pythonforeveryone.com" # not valid, no @ if "@" not in email_address: print("Error!")
Output:
Error!