I'm still very new to Python. I'm working on my assignment but I still do not understand how the find() method work when extracting emails. The purpose of the function is to extract the company of the email (i.e., everything after the @ sign but before the .) using the find() method.

Example Usage:

extract_company("[email protected]") should return "uber"

So I understand that find(subtr, start, end). And I know how it should work, but in only one condition. However, this asked to find the company name in between @ and .

This is the code I have been working so far but it still gives me an assertionerror

def extract_company(email):
    return email.find('@' and '.') 

3 Answers

Udit Hari Vashisht On

find() method find for the particular string etc. What you are looking for is the word after @ and before .com, so you can use regex for that. However there is another geeky method using split to do that

def extract_company(email):
    return email.split('@')[-1].split('.')[0]

What this code will be doing, it will be splitting the email address at '@' and then taking the last item and splitting it a '.' and then taking the first list item, which will be your company name.

If you intend to use regex. Then the code will be :-

import re
def extract_company(email):
    x= re.findall(r"\@(.*?)\.", "[email protected]")
    return x[0]

What it does is it looks for all the characters between '@' and '.' and then returns the list of that characters. And the element in that list is the company name.

zero On
def extract_company(email):
    index_of_at = email.find('@')

    return email[index_of_at+1:email.rfind('.')]

Assuming you're required to use base Python, this is one way.

  1. Store the index where the first '@' is found.
  2. slice the string such that you start at the letter after '@' (i.e. index_of_at + 1) and the last '.'(i.e. email.rfind('.')).
bumblebee On

You can use the inbuilt re library to extract the company name from the email.


import re
email = "[email protected]"
print(re.findall(r'\w+[.]', email)[0][:-1])