0

Hey guys so I tried looking at previous questions but they dont answer it like my teacher wants it to be answered. Basically i need to get a string from a user input and see if it has: at least one of [!,@,#,$,%,^,&,*,(,)] (non-letter and nonnumeric character) o Create a list for these special characters

I have no idea how to make a def to do this. Please help!

2 Answers 2

1

You should probably look into Regular expressions. Regular expressions allow you to do many string operations in a concise way. Specifically, you'll want to use re.findall() in order to find all special characters in your string and return them. You can check if the returned list has length 0 to check if there were any special characters at all.

With regards to building the regular expression to find special characters itself... I'm sure you can figure that part out ;)

Sign up to request clarification or add additional context in comments.

1 Comment

Okay so I tried using findall and the if statement that uses just ignores it now. This is what i did ' def containsSpecial(password): specialChar = ['!','@','#','$','%','^','&','*','(',')'] if re.findall(specialChar, password) return "true"'
0

Please try the below

import re
inputstring = raw_input("Enter String: ")
print inputstring
print "Valid" if re.match("^[a-zA-Z0-9_]*$", inputstring) else "Invalid"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.