Knowee
Questions
Features
Study Tools

Write a function called count_capitals that takes a string argument and returns the number of capital (upper case) letters in the string. The function should look very similar to the one you wrote for Exercise 1(a) in Lab 4.For this problem, you will need to determine if a letter is a capital. It will be helpful to know that in the unicode character encoding, the capital letters (of the English alphabet) are ordered sequentially; that is ord('A') + 1 == ord('B'), ord('A') + 2 == ord('C'), etc, up to ord('A') + 25 == ord('Z'). (Alternatively, have a look at the documentation of python's string methods; there are several that help you do things with letter case.)

Question

Write a function called count_capitals that takes a string argument and returns the number of capital (upper case) letters in the string. The function should look very similar to the one you wrote for Exercise 1(a) in Lab 4.For this problem, you will need to determine if a letter is a capital. It will be helpful to know that in the unicode character encoding, the capital letters (of the English alphabet) are ordered sequentially; that is ord('A') + 1 == ord('B'), ord('A') + 2 == ord('C'), etc, up to ord('A') + 25 == ord('Z'). (Alternatively, have a look at the documentation of python's string methods; there are several that help you do things with letter case.)

...expand
🧐 Not the exact question you are looking for?Go ask a question

Solution

Here is a step-by-step guide to writing the function count_capitals:

Step 1: Define the function and its input

def count_capitals(string):

Step 2: Initialize a counter to 0. This counter will keep track of the number of capital letters in the string.

    count = 0

Step 3: Iterate over each character in the string. For each character, check if it is a capital letter. If it is, increment the counter by 1.

    for char in string:
        if char.isupper():
            count += 1

Step 4: After all characters in the string have been checked, return the counter.

    return count

So, the complete function would look like this:

def count_capitals(string):
    count = 0
    for char in string:
        if char.isupper():
            count += 1
    return count

This function uses the isupper() method, which returns True if the string contains only upper case letters and False otherwise. It iterates over each character in the string, checks if it is an upper case letter, and if it is, increments the counter. At the end of the function, it returns the counter, which represents the number of capital letters in the string.

This problem has been solved

Similar Questions

Write a C program to read an English Alphabet through keyboard and display whetherthe given alphabet is in upper case or lower case.

#Write a Python function count_vowels(text) that takes a string as input and returns the count of vowels (a, e, i, o, u) in the input string, ignoring case.

Define a class to accept a string and convert it into uppercase. Count and display thenumber of vowels in it.

Suggest appropriate functions for the following tasks –(a) To check whether the string contains digits.(b) To find the occurrence a string within another string.(c) To convert the first letter of a string to upper case.(d) To convert all the letters of a string to upper case.(f) To check whether all the letters of the string are in capital letters.(g) to remove all the white spaces from the beginning of a string

Write a program to check whether a given character is Alphabet or not using if else statement Note: Check for both upper and lower case characters

1/3

Upgrade your grade with Knowee

Get personalized homework help. Review tough concepts in more detail, or go deeper into your topic by exploring other relevant questions.