Problem Statement:
We are given a string, we need to check whether the string is a valid username or not. To be a valid username, the string should satisfy the following conditions:
- The string should only contain letters, numbers, or underscore(s).
- It should not start with a number.
- It should not end with an underscore.
- Its length should be greater than equal to 4 and less than equal to 25.
Code for Validation in Python:
def check_username(s): alphabets = list('abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ') numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0] if (type(s[0]) != str): return 'Invalid Username1' if (len(s)<4): return 'Invalid Username2' if (len(s)>25): return 'Invalid Username3' if (s[-1] == '_'): return 'Invalid Username4' for i in s: if (i in alphabets) or (i == '_') or (i in numbers): continue return 'Invalid Username5' return 'Valid Username' t = int(input('Enter number of test cases: ')) for i in range(t): s = input('\nEnter username: ') s = list(s) for i in range(len(s)): try: s[i] = int(s[i]) except: continue print(check_username(s), '\n')
Output:

Also Read:
- Hyphenate Letters in Python
- Earthquake in Python | Easy Calculation
- Striped Rectangle in Python
- Perpendicular Words in Python
- Composite Number in Python
- Greatest Among Four Numbers in Python
- Reverse the sentence in Python
- Denominations in Python
- Min and max values in an array in JavaScript
- Keyboard events in JavaScript
- Reaching Ground Floor in Python
- Number of Moves in Python
- Starks Adventure in Python
- Neutralization in Python | Assignment Expert
- Free shipping in Python
- Raj has ordered two electronic items Python | Assignment Expert
- First Place in Python
- Triples with Properties in Python
- Nested list Indexing Python
- Team Points in Python
- Two words combination in Python
- ID Card in Python
- Cipher with a key in Python | Assignment Expert
- Multiple of 5 in Python
- Sandglass Star in Python
- Multiple of 3 in Python | Assignment Expert
- Ticket selling in Cricket Stadium using Python | Assignment Expert
- Sum of List Elements in Python
- All possible subsets in Python
- Names and Nicknames in Python