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):continuereturn '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:continueprint(check_username(s), '\n')
Output:

Also Read:
- Hyphenate Letters in Python
- Earthquake in Python | Easy Calculation
- Striped Rectangle in Python
- Perpendicular Words in Python
- Free shipping in Python
- Raj has ordered two electronic items Python | Assignment Expert
- Team Points in Python
- Ticket selling in Cricket Stadium using Python | Assignment Expert
- Split the sentence in Python
- String Slicing in JavaScript
- First and Last Digits in Python | Assignment Expert
- List Indexing in Python
- Date Format in Python | Assignment Expert
- New Year Countdown in Python
- Add Two Polynomials in Python
- Sum of even numbers in Python | Assignment Expert
- Evens and Odds in Python
- A Game of Letters in Python
- Sum of non-primes in Python
- Smallest Missing Number in Python
- String Rotation in Python
- Secret Message in Python
- Word Mix in Python
- Single Digit Number in Python
- Shift Numbers in Python | Assignment Expert
- Weekend in Python
- Shift Numbers in Python | Assignment Expert
- Temperature Conversion in Python
- Special Characters in Python
- Sum of Prime Numbers in the Input in Python