String Format Checking in Python - python

While preparing for my AS-Level Computer Science exam I came across a question in the pre-release material:
Prompt the user to input a User ID and check if the format of the ID corresponds with pre-defined formatting rules and output accordingly.
The Format (In order):
One upper case letter
Two Lower case letters
Three numeric characters (digits)
Example: "Abc123"
I came up with a solution using my language of choice(Python), however, I was wondering if there is a more elegant or better way to solve this. Especially the third check.
Here is my code:
#Task 2.2
u_id = input("Input User ID: ") #DECLARE u_id : string
numbers = [str(num) for num in range(10)]
#Checking if final 3 characters of User ID (u_id) are digits
for i in list(u_id[3::]):
if i not in numbers:
digit_check = False #DECLARE digit_check : bool
break
else:
digit_check = True
#User ID format check
if (u_id[0:1].isupper() == True) and (u_id[1:3] == u_id[1:3].lower()) and (digit_check == True):
print ("Correct Format")
else:
print ("Wrong Format")
Ignore the DECLARATION comments. They are an exam requirement.
Thanks

If you are allowed to import re:
import re
u_id = input("Input User ID: ") #DECLARE u_id : string
rex = re.compile("^[A-Z][a-z]{2}[0-9]{3}$")
if rex.match(u_id):
print("Correct format")
else:
print("Incorrect")
Explanation of expression:
^ represents the beginning of a string.
[A-Z] is a range, containing all uppercase letters (in the English alphabet).
[a-z] is a range, containing all lowercase letters.
[0-9] is a range, containing all numbers.
{n} specifies that n items (items are whatever is before the curly brackets) will be matched.
$ represents the end of the string.
Also, you can see more detailed explanations and test arbitrary strings against this regular expression here.

If you want to solve it without regular expressions (mind you, in this case they are the right tool!), you could do something like this:
id_format = [
"ABCDEFGHIJKLMNOPQRSTUVWXYZ", # or string.ascii_uppercase etc.
"abcdefghijklmnopqrstuvwxyz",
"abcdefghijklmnopqrstuvwxyz",
"0123456789",
"0123456789",
"0123456789",
]
def check(input):
# check for same length
if len(input) != len(id_format):
return False
for test, valid in zip(input, id_format): # itertools.zip_longest can make
if test not in valid: # the length check unnecessary
return False
return True
check("Abc123") # True
check("abc123") # False

Related

How python check data from user input that not include number and special characters

how can find all the special characters for using to check the user input name? I don't mean only the special characters on the keyboard, but all the special characters in Office, as well as all other languages, etc. Is there a function in the library for this? (not isalpha or isalnum its not perfect)
Also, I want to find which characters as special or digits.
update: this is a simple of this problem, isalpha does not help me because users can input this (Siavashستیبیلانذس). isalpha does not recognize language.
name = input("What is your name? ")
if name.isalpha():
print("Great!")
else:
print('Please check spell and insert correct Name!')
print("Your name is:", name)
Looks like name should be only ASCII alphabet characters
This will satisfy desired conditions:
def is_name_correct(s):
# isascii - check if string consists only ascii characters
# isalpha - check if string consists only alphabet characters
return s.isascii() and s.isalpha()
print("Siavashستیبیلانذس", is_name_correct("Siavashستیبیلانذس"))
print("123", is_name_correct("123"))
print("aBc", is_name_correct("aBc"))
print("a BBB", is_name_correct("a BBB"))
print("a!", is_name_correct("a!"))
output:
Siavashستیبیلانذس False
123 False
aBc True
a BBB False
a! False
If you're running a python version under python 3.7, .isalpha() won't work due to it getting added in the 3.7 update. In which case I recommend using my code
Code
def is_name_correct(s):
s = str(s)
alphabet = list("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
return all(ord(c) < 128 for c in s) and all(x == True for x in [d in alphabet for d in s])
to_check = ["English","Français","русский"]
for i in to_check:
print(i,":",is_name_correct(i))
Output
English : True
Français : False
русский : False

Python Regex validation

I am brand new to Python.
I'm trying to ensure a username contains ONLY alpha characters (only a-z). I have the below code. If I type digits only (e.g. 7777) it correctly throws the error. If I type numbers and letters mix, but I START with a number, it also rejects. But if I start with a letter (a-z) and then have numbers in the string as well, it accepts it as correct. Why?
def register():
uf = open("user.txt","r")
un = re.compile(r'[a-z]')
up = re.compile(r'[a-zA-Z0-9()$%_/.]*$')
print("Register new user:\n")
new_user = input("Please enter a username:\n-->")
if len(new_user) > 10:
print("That username is too long. Max 10 characters please.\n")
register()
#elif not un.match(new_user):
elif not re.match('[a-z]',new_user):
print("That username is invalid. Only letters allowed, no numbers or special characters.\n")
register()
else:
print(f"Thanks {new_user}")
Why don't you use isalpha()?
string = '333'
print(string.isalpha()) # False
string = 'a33'
print(string.isalpha()) # False
string = 'aWWff'
print(string.isalpha()) # True
in your code, uf, un and up are unused variables.
the only point where you validate something is the line elif not re.match('[a-z]',new_user):, and you just check if there is at least one lowercase char.
To ensure that a variable contains only letters, use: elif not re.match('^[a-zA-Z]{1,10}$',new_user):
in the regex ^[a-zA-Z]{1,10}$ you find:
^ : looks for the start of the line
[a-zA-Z] : looks for chars between a and z and between A and Z
{1,10} : ensure that the char specified before (letter) is repeated between 1 and 10 times. As LhasaDad is suggesting in the comments, you may want to increase the minimum number of characters, e.g. to 4: {4,10}. We don't know what this username is for, but 1 char seems in any case too low.
$ : search for the end of the line
Since you were looking for a RegEx, I've produced and explained one, but Guy's answer is more pythonic.
IMPORTANT:
You're not asking for this, but you may encounter an error you're not expecting: since you're calling a function inside itself, you have a recursive function. If the user provides too many times (more than 1000) the wrong username, you'll receive a RecursionError
As the re.match docs say:
If zero or more characters at the beginning of string match the regular expression pattern, return a corresponding match object.
That's exactly what's happening in your case: a letter in the beginning of the string will satisfy the match. Try the expression [a-z]+$ which will make sure that the match expands till the end of the string.
You can check the length on the same go: [a-z]{1,10}$.

What does lambda do to stop switcher from executing all functions

I am learning python, I used the the switcher dictionary. I encountered the problem of all the functions of the dictionary getting executed.
'''
Created on Mar 10, 2019
#author: priti
Questions
1) Write a program that accepts a comma separated sequence of words as input and prints the words in a comma-separated sequence after sorting them alphabetically.
Suppose the following input is supplied to the program:
without,hello,bag,world
Then, the output should be:
bag,hello,without,world
2) Write a program that accepts a sentence and calculate the number of upper case letters and lower case letters.
Suppose the following input is supplied to the program:
Hello world!
Then, the output should be:
UPPER CASE 1
LOWER CASE 9
Hints:
In case of input data being supplied to the question, it should be assumed to be a console input.
3) (HINT use re i.e. import re (re is regular expression library/functions).
Question:
A website requires the users to input username and password to register. Write a program to check the validity of password input by users.
Following are the criteria for checking the password:
1. At least 1 letter between [a-z]
2. At least 1 number between [0-9]
1. At least 1 letter between [A-Z]
3. At least 1 character from [$##]
4. Minimum length of transaction password: 6
5. Maximum length of transaction password: 12
Your program should accept a sequence of comma separated passwords and will check them according to the above criteria. Passwords that match the criteria are to be printed, each separated by a comma.
Example
If the following passwords are given as input to the program:
ABd1234#1,a F1#,2w3E*,2We3345
Then, the output of the program should be:
ABd1234#1
'''
# method used to perform operation regarding the first question
from unittest.util import sorted_list_difference
import re
def comma_separated():
sorted_list=[]
rawData = input("Please enter comma separated words\n")
rawData = rawData.strip().split(",")
for item in rawData:
item.strip()
sorted_list.append(item)
sorted_list.sort()
for item in sorted_list:
print(item)
def cal_num_upper_lower():
upperCase = 0
lowerCase = 0
rawData = input("Please enter string\n")
rawData.strip().split(",")
for i in rawData:
#print(i)
if(i.islower()):
lowerCase +=1
if(i.isupper()):
upperCase +=1
print("UPPER CASE = " + str(upperCase))
print("LOWER CASE = " + str(lowerCase))
# method used to perform operation regarding the third question
def check_regular_expression():
sorted_list=[]
password = input("Enter string to test: ")
password = password.strip().split(",")
for item in password:
item.strip()
sorted_list.append(item)
#
# print("match")
for item in sorted_list:
if(re.match(r"^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[##$]){6,12}", item)):
print(item)
def choose_the_option(argument):
switcher = {
1: lambda: comma_separated(),
2: lambda: cal_num_upper_lower(),
3: lambda: check_regular_expression(),
}
return switcher.get(argument,lambda: "Please enter a valid number") ()
print("Select one of the options")
option = int(input("1:Sort sequence of words\n2:Calculate Number of upper and lower case\n3:Check Regular Expression\n Please enter:"))
choose_the_option(option)
So I followed this question
Executing functions within switch dictionary
Here is is mentioned that we need to use lambda to ensure only the correct method is called. While the solution worked perfectly,I could not get what exactly happens when we use lambda in switcher.
Thanks.

A check text numbers, alphabets, letters upper case and lower case except symbols

I need guys your help.
I can't understand what to use either list or set. List is more efficient. dictionary also need index. but my problem is text should be string so variable must equal to text as string. I can't D=['a','b','c'].
text gives me error because it can't compare them all except individual and i must create such as abc or word example as _success and confirm its in the list to be true.
This is my code so far but i have problem which is now it accepts numbers and letters and symbols. Symbols such as !##$% should be returning False.
Having it as own function works but i need it in the if statement.
return text.isalnum() doesn't work in the if statement. Thats my problem symbols should be false.
def check(text):
if text== '':
return False
if text.isalpha() == text.isdigit():
return True
else:
return text.isalnum()
def main():
text = str(raw_input("Enter text: "))
print(check(text))
main()
output problem.
Enter text: _
False
_ is suppose to be one of the symbols True. Example _success123 is True
!##$% is suppose to be false but its showing as True as output Another example is !##A123. This output is False.
The code up there does accept the underscore and letter and number
output:
_success123
but problem is also accepts !##$ as True.
return text.isalnum() Does deny the symbols but its not working in the if statement.
It's an overkill, but you can use Regex. It's easy to add new chars (e.g. symbols):
import re
def check(text):
return re.match('^[a-zA-Z0-9_!]*$', text)
text = str(raw_input("Enter text: "))
print(check(text))
If you want to avoid a regular expression, you could use Python sets:
allowed = set('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_0123456789')
def check(text):
return not len(set(text) - allowed)
for text in ['_success123', '!##$%']:
print(text, check(text))
This converts your text into a set of characters and removes all the characters that are allowed. If any characters remain then you know it is invalid. For two examples, this gives:
_success123 True
!##$% False

How would python recognise if a string is in the right format? (LLNN.LLL)

I am trying to write a program that would check if the letters and numbers in a string are in the right order, and the example I am using to write it is a car registration plate. Is it possible to check if the letters and numbers in a string correspond to a format of where they are in relation to each other?
For example:
The user inputs a car's registration, and the program checks if it is a standard number plate or not, and if it isn't, the program tells the user. The format's allowed would be from a standard UK registration plate (LLNN.LLL), and the strings could be up to 10 characters long.
You should have a look at regular expressions
For example number plates in my country are 3 letters and 3 numbers (ABC-123) so the regex string would be ([a-zA-Z]{3})-([0-9]{3})
You can use http://pythex.org/ to test regex patterns.
I try to give an answer but it is likely that this is not what you want:
import re
string1 = "(LLNN.LLL)"
string2 = "(abcdefgh)"
reg = re.compile(r'\([LN]{4}\.[LN]{3}')
print bool(re.match(reg, string1))
print bool(re.match(reg, string2))
This matches all the strings that consist of (, 4 times L or N, a dot and 3 times L or N.
this code takes the registration something like xjk649 and tests it, and prints when its false:
iReg = input('Registration: ')
test = ''
for i in range(0,len(iReg)-3):
if 'abcdefghijklmnopqrstuvwxyz'.find(iReg[i]) != -1:
print('succes first three charracters')
else:
test = 'WRONG'
for j in range(3,len(iReg)):
if '0123456789'.find(iReg[j]) != -1:
print('succes last three charracters')
else:
test = 'WRONG'
if test == 'WRONG':
print('false resgistration')
hope this helped
Thanks

Categories

Resources