Validate a Username Manually

Hard ⏱ 18 min 30% acceptance ★★★★★ 4.8
Write validate_username(username) enforcing all of these rules without the re module: length between 5 and 15 characters inclusive; must start with a letter; may only contain letters, digits, and underscores; and must not contain two consecutive underscores. Return True only if every rule passes.

Examples

Example 1
Input
username = 'rahul_99'
Output
True
Explanation

Starts with a letter, valid characters only, length 8, no double underscore.

Example 2
Input
username = '9rahul'
Output
False
Explanation

Starts with a digit.

Example 3
Input
username = 'ab'
Output
False
Explanation

Shorter than the 5-character minimum.

Example 4
Input
username = 'ra__hul'
Output
False
Explanation

Contains a consecutive double underscore.

Constraints

  • username may be empty.

Topics

StringsValidation

Companies

TCSWiproInfosys

Hints

Hint 1

Check length first with a simple bound test.

Hint 2

Check `username[0].isalpha()`.

Hint 3

Loop the rest checking `.isalnum() or ch == '_'`.

Hint 4

Finally check `'__' not in username`.

Loading the Python runtime… Run executes your code and shows printed output; Submit checks your function against this problem's examples.