Ryan Leslie - 1 month ago 10

Python Question

so I've been assigned an assignment (obviously) to check if an input that a user has entered is formatted correctly, in the way AA99AAA (where A is a letter and 9 is a number from 0 to 9). So for the first character in the input, it would have to be a letter or python would return some sort of error and so on, you get the idea. I've got no clue where to start with this, I've tried looking around and haven't found anything - I guess I just don't know what it is I'm looking for. Any pointers would be greatly appreciated, thanks!

Answer

To do this, you could split the string into 3 parts (the first group of letters, the numbers, and then the second group of letters). Then you can use `s.isalpha()`

and `s.isnumeric()`

.

For example:

```
while True:
c=input('Password: ')
if len(c)==7 and c[:2].isalpha() and c[2:4].isnumeric() and c[4:].isalpha():
break
else:
print('Invalid input')
print('Valid input')
```

Source (Stackoverflow)

Comments