Determining if a letter in a list of letters is uppercase

Question:

I am attempting to write a program where it takes a list input by the user and determines if the letter in the list is uppercase or not. This is the code I have so far.

#  Write a Python function to count how many letters in a string are in uppercase.

print("Hey, do you wanna see a cool trick I can do? I can count how many letters in a list of letters are uppercase!")
print("Here, I can prove it!")
print("Gimme a list of letters!")

letter = input("Enter a list of letters, seperating each by a comma. (put both uppercase and lowercase!) ")
letter_list = letter.split(",")
print("The list you gave was...")
print(letter_list)
print("And if we seperate those, we get...")
for letter in letter_list:
    print(letter)
    
print("Now, I'm going to count how many of those are uppercase!")

I am currently trying to get it to check if the letter is uppercase properly. I have tried an if statement nested in a for loop, but I’m unsure if I did it right, because it didn’t work. The code I input was:

for letter in letter_list:
    if letter.isupper == True:
        capital = capital + 1

capital is what is being printed back to the user showing the amount of capital letters.

Asked By: JuicyMango17

||

Answers:

Your code has a few problems. The first is that you are not declaring capital before incrementing it. You should have capital = 0 before the for a loop. Second, you are comparing with ==, but you should use is instead. Lastly, to check if a letter is uppercase, you must call letter.isupper() (not letter.isupper). If you don’t use parentheses, then it is referring to the function itself and you’ll get all kinds of weird errors.

Here is the code that I got to work:

# ... rest of your code ...

capital = 0
for letter in letter_list:
    if letter.isupper() is True:
        capital = capital + 1
print("Number of capital letters: " + capital)
Answered By: Michael M.

Since isupper is a string method, you need the () at the end of the method call.
So it would look like this

for letter in letter_list:
    if letter.isupper() == True:
        capital = capital + 1

Also, make sure you initialize capital before you start the for loop.

Answered By: Jared Hanson

Alternatively, you can try this generator expression way:

count = 0
letter_list = ['Fountain', 'is', 'ForEver', 'Young']

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

        
print(count)
# 4
# Generator Expression - in one shot:

count = sum(char.isupper() for word in letter_list for char in word)
print(count)
4
Answered By: Daniel Hao

As an suggestion or alternative solution, you can use Regex;

import re

str = "Hey, do you wanna see a cool trick I can do? I can count how many letters in a list of letters are uppercase!"

r1 = re.findall(r"[A-Z]", str)
print(len(r1))
Answered By: POURiA
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.