Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How would I get a function to return True or False if the argument that is inputted is a symbol? (ex. %$#@)

As a beginner practice I have to create a function that returns True if ALL the letters in the string are uppercase. I am able to do that but it is returning False if only a string of symbols is inputted (ex. #$%) when it should be returning True.

Here is the code, it will return True if all letters are capital & include symbols but will not return True if only containing symbols.

def is_uppercase(inp):
    if inp.isupper() and inp != int:
        return True
    else:
        return False

The exact input that is failing is $%&.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Your problem is, .isupper() will return False for anything that is not an uppercase character, including symbols and digits. You will need to use the inverse (not) of .islower() instead. You should also iterate over the string and check each character individually:

def is_uppercase(inp):
    return not any(i.islower() for i in inp)

The (i.islower() for i in inp) part is a generator expression that iterates over the string and returns the .islower() value of the next character every time it’s called.

any() will compute every value of the generator expression until it finds True.

not will inverse the output.

Basically it will return False if it finds a non-uppercase character, and True if it can’t.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading