Detect Capital using Python

The problem of detecting capital is a popular question in coding interviews. A word is written in capital letters if all of its letters are uppercase or if only the first letter is uppercase and the rest are lowercase. In coding interviews, you will be given a word as input, and you need to detect whether the first letter or all the letters are capitalised or not. So, if you want to know how to solve this question, this article is for you. In this article, I’ll take you through how to detect capital using Python.

Detect Capital Problem

In the detect capital problem, you will be given a word as input, and you need to check if:

  1. All the letters are uppercase: return True;
  2. All the letters are lowercase: return True;
  3. Only the first letter is uppercase: return True;
  4. Some letters are uppercase, and some are lowercase: return False;
  5. The first letter is lowercase with an uppercase letter in the word: return False;

So in this problem, we need to check if the words containing capital letters are using capital letters correctly or not. For example, look at the input and output values of this problem shown below:

  • Input: “USA” | Output: True
  • Input: “aman” | Output: True
  • Input: “Google” | Output: True
  • Input: “FlaG” | Output: False

Detect Capital using Python

I hope you have understood what the problem of detecting capital means. Below is how you can detect capital using the Python programming language:

def detectCapitalUse(word):
    if word.isupper():
        return True
    elif word.islower():
        return True
    elif word.istitle():
        return True
    else:
        return False
print(detectCapitalUse("USA"))
Output: True

Below is how the above code works:

  1. First, it checks if all the letters in the word are uppercase using the isupper() method. If they are all uppercase, the function returns True.
  2. If all the letters are not uppercase, the function checks if they are all lowercase using the islower() method. If they are all lowercase, it returns True.
  3. If all the letters are neither uppercase nor lowercase, the function checks if only the first letter is uppercase and the rest are lowercase using the istitle() method. If the first letter is uppercase and the other letters are lowercase, the function returns True.
  4. If none of the above conditions is satisfied, the function returns False.

So this is how you can detect capital using Python. You can find many more practice questions for coding interviews solved and explained using Python here.

Summary

In the detect capital problem, we need to check if the words containing capital letters are using capital letters correctly or not. A word is written in capital letters if all of its letters are uppercase or if only the first letter is uppercase and the rest are lowercase. I hope you liked this article on detecting capital using Python. Feel free to ask valuable questions in the comments section below.

Aman Kharwal
Aman Kharwal

I'm a writer and data scientist on a mission to educate others about the incredible power of data📈.

Articles: 1498

4 Comments

Leave a Reply