How can I get the characters that come after the @ on email value with python?

54 Views Asked by At

I want to know what comes especificaly after the "@" on an email value that is in an input with python, what are ways to do it?

Examples: email: input("Write your email: ")

User input: Write you email: [email protected]

[email protected] result: hotmail.com

[email protected] result: gmail.com

I have no idea on how I can do it, but I would try manging strings and using len()

4

There are 4 best solutions below

0
Suramuthu R On BEST ANSWER

You can do this way:

email = '[email protected]'

#Get the index of '@' with find method and add 1 to it
idx = email.find('@') + 1

#Get the character with email[idx]
required_character = email[idx]

print(required_character) #output : 'h'

#to get the domain name after the symbol @
domain = email[idx:]
print(domain) #output : hotmail.com
0
RecoilGaming On

You can do input("Write your email: ").split("@")[1] to split the string into a list of the two parts separated by @ and take the second part with [1] (the hotmail.com or gmail.com).

0
Rahul Bhoyar On

email = input("Write your email: ")

at_index = email.find("@")

if at_index != -1:

domain = email[at_index + 1:] print(f"Domain: {domain}") else: print("Invalid email format")

0
Rich - enzedonline On

It's handled by the following in Django validators:

 username_part, domain_part = value.rsplit("@", 1)

https://www.w3schools.com/python/ref_string_rsplit.asp

In [1]: '[email protected]'.rsplit('@', 1)
Out[1]: ['abc', 'xyz.com']