Search code examples
pythonreverse

Reverse username from email


I created a script whereas the output should be "ACER,ONE" however I am stuck how can I place a reverse the username which I ended up "ONE,ACER" as result.

email='[email protected]'
index = email.index("@")
email_id = email[:index]
email_id = email_id.upper()
print(email_id.replace(email_id[3],","))
# > ONE,ACER

Solution

  • ",".join("ACER,ONE".split(",")[::-1])
    

    In your case, you could do:

    print(",".join(email.split("@")[0].split(",")[::-1]))
    

    as @ox5453 mentioned in the comments, you can also use the built-in reversed method for readability:

    print(",".join(reversed("ACER,ONE".split(","))))
    print(",".join(reversed(email.split("@")[0].split(","))))