简体   繁体   中英

Find if a sentence is completely capital?

I have a variable, and i want to check if it is completely in capital.

loc = "SAN JOSE"

How can i check if the entire phrase in capital. I know how to check if the individual letter is capital. It can have as many words.

loc[0].isupper()
>>True

Python doesn't differentiate between strings and characters. Just call the same method on the whole string:

loc.isupper()
>>> "SAN JOSE".isupper()
True
>>> "SAN jOSE".isupper()
False

You can just call the isupper function, like loc.isupper() . Punctuation does not affect this. Let me demonstrate:

>>> a = "CHEESE-CAKE"
>>> a.isupper()
True
>>> b = "SAN JOSE"
>>> b.isupper()
True

Take a look at the documentation for it. isupper only takes alphabetic characters into consideration. For example:

>>> a = "CHEESE123123CAKE"
>>> a.isupper()
True

The following example serves no other purpose but to have some fun:

>>> ">-----+>A<+------<".isupper()
True
>>> ":P".isupper()
True
loc = "Your Text"

print upper_text == loc.upper()

or you can check with the ASCII value

loc = "SAN JOSE"
if loc == loc.upper():
    print 'All Upper'

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM