Validating existence of symbols in input - python

So I want to validate the input of a first name by checking if it has invalid characters. It works for blank entries and numbers but won't pick up any of the symbols.
import re
while True:
f_name = input('First name: ').strip().lower()
if f_name == '':
print("* you haven't entered anything.")
continue
elif re.search('[0-9]', f_name):
print("* entry can't contain numbers. please try again with only letters.")
continue
elif re.search('[~!##£€$¢¥§%^&*/()\\-_+={}[]:;"\'<>,.?]', f_name):
print("* entry can't contain symbols. please try again with only letters.")
continue
else:
break
It works when only a few symbols are in the brackets but I need to check a wide range, is there any way to fix this?

I believe you can use [^\w/]|_.
[^\w/] matches alphanumeric + _ as well as /
|_ excludes underscore since _ is included by default with alphanumeric characters with [^\w/]:
import re
while True:
f_name = input('First name: ').strip().lower()
if f_name == '':
print("* you haven't entered anything.")
continue
elif re.search('[0-9]', f_name):
print("* entry can't contain numbers. please try again with only letters.")
continue
elif re.search('[^\w-]|_', f_name):
print("* entry can't contain symbols. please try again with only letters.")
continue
else:
break

Check for any of characters not being alphanumeric like:
any(not c.isalnum() for c in mystring)
This link may also help further: How to check if a string has ANY special characters?

'[~!##£€$¢¥§%^&*/()\\-_+={}[]:;"\'<>,.?]'
is not a valid regex. Well, at least not the regex that you're looking for. You have some unescaped characters in there, such as ] and " that messes it up. You can put it into a regex tester to see what I mean.
Doing it with a regex isn't the best option anyways. You have plenty of options for doing this, depending on your desired behavior. As others have mentioned, you could use isalnum() and isalpha() to check if a string only contains alphanumeric or alphabetical characters.
If you only want to exclude a special list of symbols and not other special symbols, you can use the intersection of two sets:
DISALLOW_SYMBOLS = set("~!##£€$¢¥§%^&*/()\\-_+={}[]:;\"'<>,.?")
f_name = input('First name: ').strip().lower()
name_set = set(f_name)
symbols = DISALLOW_SYMBOLS.intersection(name_set)
if symbols:
print("* entry contains these symbols that are not allowed: ", symbols)

Related

How to make a string that only includes spaces be an invalid input in python

I'm a newbie in coding using python and I'm trying to write a piece of small code that prints whatever you input in the terminal, but I don't want anything that only includes spaces and no other characters or doesn't even have an input to be printed. Here's the code:
while True:
my_name= input("> ")
if "" in my_name:
print("I don't know what that means. Please choose a valid answer.")
else:
print(f"Ah, I see, so your name is {my_name}?")
would love to have multiple solutions to this, and thank you if you helped :)
https://docs.python.org/2/library/stdtypes.html#str.isspace
I think this should do the trick
if my_name.isspace():
print("Your name is full of spaces")
Python isspace() method is used to check space in the string. It returns true if there are only white space characters in the string. Otherwise it returns false.
ex:
# Python isspace() method example
# Variable declaration
str = " " # empty string
# Calling function
str2 = str.isspace()
# Displaying result
print(str2)
There's one way of doing that by using exceptional handling for no input using try - except block and a flag variable that checks if input doesn't contain only spaces.
try:
flag = False
my_name = input()
for i in my_name:
if i != ' ':
flag = True
if flag:
print(my_name)
else:
print('Invalid input')
except:
print('No input')
The other ways can be using the built in function strip() which removes the trailing and leading spaces in input.
If the input has only spaces then strip() will remove all of them resulting in an empty string.
try:
my_name = input()
if my_name.strip() == '':
print('Invalid input')
else:
print(my_name)
except:
print('No input')
Like this, you can use bools:
while True:
my_name= input("> ")
if my_name.strip():
print(f"Ah, I see, so your name is {my_name}?")
else:
print("I don't know what that means. Please choose a valid answer.")

Prevent input() from being anything but alphabet characters

I am attempting to make a program for the sake of self-knowledge. I want to ask the user what their name is, and I only want the user to be able to use letters from the alphabet to answer, or only strings. I do not want them to be able to answer with numbers, symbols, etc.
def cc():
name = (input("""Hello, what happens to be your first name?
> """))
if type(name) is str:
print("You have entered a name correctly.")
elif type(name) is int:
print("Your name cannot be an integer. Try again.")
cc()
cc()
You can enforce this requirement using str.isalpha. From the documentation:
Return true if all characters in the string are alphabetic and there is at least one character, false otherwise. Alphabetic characters are those characters defined in the Unicode character database as “Letter”, i.e., those with general category property being one of “Lm”, “Lt”, “Lu”, “Ll”, or “Lo”. Note that this is different from the “Alphabetic” property defined in the Unicode Standard.
Here is an example program:
while True:
name = input('Enter a name using only alphabetic characters: ')
if name.isalpha():
break
Demo:
Enter name using only alphabetic characters: Bo2
Enter name using only alphabetic characters: Bo^&*(
Enter name using only alphabetic characters: Bob
Note this method will not work for people with hyphenated names such as "Anne-Marie".
I agree that this question is a little misleading. However, with what you have said, you just need to use a regex to accomplish this.
import re
...
if not re.findall('[^a-zA-Z]', 'abc1'):
print("You have entered a name correctly.")
else
print("Your name cannot be an integer. Try again.")
cc()

Python - Validation to ensure input only contains characters A-Z

I'm creating a program in Python 3.x where it asks the user for their first name and then last name and stores these in variables which are then concatenated into one variable:
firstName = input("What's your first name? ")
lastName = input("What's your first name? ")
name = firstName + " " + lastName
I tried:
while True:
try:
firstName = input("What's your first name? ")
lastName = input("What's your first name? ")
name = firstName + " " + lastName
break
except ValueError:
print("That's invalid, please try again.")
which ensures that a string is inputted, but inputting 'bob38', '74' or '][;p/' all count as string values, so these would be accepted, which is not what I want to happen.
I want valid input to only contain the letters A-Z/a-z (both uppercase and lowercase), and if the input contains anything else, an error message is outputted (e.g. 'That's invalid, try again.') and the user is asked the question again. How would I do this?
What you want to do is actually check if your string is not isalpha() and output an error and continue your loop until you get a valid entry.
So, to give you an idea, here is a simple example:
while True:
name = input("input name")
if name.isalpha():
break
print("Please enter characters A-Z only")
print(name)
Note, that as mentioned in the comments to this question, this is limited to only names that contain letters. There are several valid names that will contain ' and - that should probably be considered when wanting to validate names.
There is another solution. It's not as efficient as using re or isalpha but allows you to easily customize the characters you allow:
valid_characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'
# Just insert other characters into that string if you want to accept anything else.
while True:
firstName = input("What's your first name? ")
if all(char in valid_characters for char in firstName):
break
print("That's invalid, please try again.")
The all checks if all characters from firstName are contained in the valid_characters string and returns False if any of them is not in it.
So to add whitespace and minus - you can alter it slightly:
valid_characters = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ -'
# whitespace is here ------------------------------^
assert all('a'<=letter<='z' or 'A'<= letter <= 'Z' for letter in name)
This will throw an error if name contains a non alphabet letter.
You can use a simple regular expression for this -
import re
is_valid_name = r'[a-zA-Z]+'
if bool(re.match(firstName, is_valid_name)) and bool(re.match(lastName, is_valid_name)):
name = firstName + lastName
else:
print('thats invalid')

String Format Checking in 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

How to test a regex password in Python?

Using a regex in Python, how can I verify that a user's password is:
At least 8 characters
Must be restricted to, though does not specifically require any of:
uppercase letters: A-Z
lowercase letters: a-z
numbers: 0-9
any of the special characters: ##$%^&+=
Note, all the letter/number/special chars are optional. I only want to verify that the password is at least 8 chars in length and is restricted to a letter/number/special char. It's up to the user to pick a stronger / weaker password if they so choose.
So far what I have is:
import re
pattern = "^.*(?=.{8,})(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[##$%^&+=]).*$"
password = raw_input("Enter string to test: ")
result = re.findall(pattern, password)
if (result):
print "Valid password"
else:
print "Password not valid"
import re
password = raw_input("Enter string to test: ")
if re.fullmatch(r'[A-Za-z0-9##$%^&+=]{8,}', password):
# match
else:
# no match
The {8,} means "at least 8". The .fullmatch function requires the entire string to match the entire regex, not just a portion.
I agree with Hammish. Do not use a regex for this. Use discrete functions for each and every test and then call them in sequence. Next year when you want to require at least 2 Upper and 2 Lower case letters in the password you will not be happy with trying to modify that regex.
Another reason for this is to allow user configuration. Suppose you sell you program to someone who wants 12 character passwords. It's easier to modify a single function to handle system parameters than it is to modify a regex.
// pseudo-code
Bool PwdCheckLength(String pwd)
{
Int minLen = getSystemParameter("MinPwdLen");
return pwd.len() < minlen;
}
Well, here is my non-regex solution (still needs some work):
#TODO: the initialization below is incomplete
hardCodedSetOfAllowedCharacters = set(c for c in '0123456789a...zA...Z~!##$%^&*()_+')
def getPassword():
password = raw_input("Enter string to test: ").strip()
if (len(password) < 8):
raise AppropriateError("password is too short")
if any(passChar not in hardCodedSetOfAllowedCharacters for passChar in password):
raise AppropriateError("password contains illegal characters")
return password
import re
password=raw_input("Please give me a password: ")
if len(re.findall("[A-Za-z0-9##$%^&+=]",password))==len(password):
print("Great password")
else:
print("Incorrect password")
If you want to run it in python 3.0 and above change raw_input with input.
import re
password = input("Enter Password")
True if (re.fullmatch(r'^[A-Za-z0-9##$%^&+=]{8,}$', password)) else False
if however you wanted to make the second bits required, you could do the following:-
True if (re.fullmatch(r'^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.[##$%^&+=])[A-Za-z0-9##$%^&+=]{8,}$', password)) else False
Note: [0-9] in python is a subset of \d so you might want to stick to [0-9] read the following post https://stackoverflow.com/a/6479605/4170558
If you wan the program to continuously run until a correct password is inputted do the following.
import re
while True:
print('Input a strong password.')
password = input()
if re.match(r'[A-Za-z0-9##$%^&+=]{8,}', password):
print('Very nice password. Much secure')
break
else:
print('Not a valid password')
This regex validate if the password:
uppercase letters: A-Z
-lowercase letters: a-z
numbers: 0-9
any of the
special characters: !#£$%^&*()_+={}?:~[]]+
re.compile(r'^.*(?=.{8,10})(?=.*[a-zA-Z])(?=.*?[A-Z])(?=.*\d)[a-zA-Z0-9!#£$%^&*()_+={}?:~\[\]]+$')
But what if the user enters another special characters out of your list? Should you include all existing special characters? Another solution may be search what we expect to be in the password separately:
numbers: 0-9
lowercase letters: a-z
uppercase letters: A-Z
any special characters
No white space
p = password
def is_valid(p):
if (len(p) >= 8 and
re.search(r'\d+', p) and
re.search(r'[a-z]+', p) and
re.search(r'[A-Z]+', p) and
re.search(r'\W+', p) and not
re.search(r'\s+', p)):
return True
else:
return False```

Categories

Resources