520. 检测大写字母

给定一个单词,判断单词的大写使用是否正确。定义在以下情况时,单词的大写用法是正确的:

1、全部字母都是大写,比如"USA"。
2、单词中所有字母都不是大写,比如"leetcode"。
3、如果单词不只含有一个字母,只有首字母大写, 比如 "Google"。
否则,我们定义这个单词没有正确使用大写字母。
注意: 输入是由大写和小写拉丁字母组成的非空单词。

示例 1:
输入: "USA"
输出: True

示例 2:
输入: "FlaG"
输出: False


思路:
Python提供了isupper(),islower(),istitle()方法用来判断字符串的大小写。

  str.isalnum() 所有字符都是数字或者字母,为真返回 Ture,否则返回 False。
  str.isalpha() 所有字符都是字母,为真返回 Ture,否则返回 False。
  str.isdigit() 所有字符都是数字,为真返回 Ture,否则返回 False。
  str.islower() 所有字符都是小写,为真返回 Ture,否则返回 False。
  str.isupper() 所有字符都是大写,为真返回 Ture,否则返回 False。
  str.istitle() 所有单词都是首字母大写,为真返回 Ture,否则返回 False。
  str.isspace() 所有字符都是空白字符,为真返回 Ture,否则返回 False。

 
 1 class Solution(object):
 2     def detectCapitalUse(self, word):
 3         """
 4         :type word: str
 5         :rtype: bool
 6         """
 7         if word.islower() or word.isupper() or word.istitle():
 8             return True
 9         else:
10             return False
11 
12 if __name__ == '__main__':
13     solution = Solution()
14     print(solution.detectCapitalUse("USA"))
 

原文地址:https://www.cnblogs.com/panweiwei/p/12682220.html