pythonemailgetat-command

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


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: XXXX@hotmail.com

JohnDoe@hotmail.com result: hotmail.com

Nick@gmail.com result: gmail.com

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


Solution

  • You can do this way:

    email = 'XXXX@hotmail.com'
    
    #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