Search code examples
pythonstringreturnsliceone-liner

Please explain return with "+1:" in it


Here is the code. 2 functions.

def get_domains(self): #returns [email protected], [email protected] etc in json. 
    if self.domain_names == None:
        r = requests.get(GET_DOMAINS_URL)
        if r.status_code != 200:
            raise ValueError("Can't get domains")
        self.domain_names = [item["name"] for item in r.json()]
    return self.domain_names

def is_valid_email(self, email):
    return email[email.find("@")+1:] in self.get_domains()

So what does part "+1:" in function is_valid_email ? How it works?


Solution

  • This is a string slicing:

    email[email.find("@")+1:]
    

    It means - take all the characters from email string from the first index after the @ char till the end of string.

    Or in simple words - extract the domain from an email address :)