Lesson 5.3 · Write it
Before the @
A functionPython or Java7 tests, 4 of them hidden
On a phone? Put the lines in order instead
Write a function that returns the part of an email address that comes before the @.
- The address contains exactly one
@. Every other character is an English letter, a digit, a dot or a hyphen. - There is at least one character before the
@. - Return every character before the
@, and nothing else. The@itself is not included.
Only the value your function returns is checked.
def username(address: str) -> str
Examples
Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.
- username("mia@example.org") should return "mia": three characters before the @ (rule 3)
- username("a@b.example") should return "a": one character before the @, the fewest allowed (rule 2)
- username("sam.lee@example.net") should return "sam.lee": a dot before the @ is part of the answer
public static String username(String address)
Running Java here comes later. You can put this answer's lines in order instead. You can still write your Java answer (it stays while this page is open) and compare it with our worked solution. Or try this exercise in Python.Checking whether Java answers can run here.
Examples
Your function is tested on 7 cases: the 3 examples below, and 4 hidden ones that check the rules above.
- username("mia@example.org") should return "mia": three characters before the @ (rule 3)
- username("a@b.example") should return "a": one character before the @, the fewest allowed (rule 2)
- username("sam.lee@example.net") should return "sam.lee": a dot before the @ is part of the answer