Read a line of input as a string and print uppercase letters

Hello:

I have this exercise from a book, Python for Everyone, first edition, Programming exercise P4.15. Page 209.

##
#  Print the uppercase letters in a string.
#

# Read input from the user.
inputStr = input("Enter a string: ")

# Find and display the uppercase letters.
for ch in inputStr :
   if ch >= "A" and ch <= "Z" :
      print(ch, end="")

print()
`indent preformatted text by 4 spaces`

Can I use ch.upper for the test? I try this but doesn’t work:

##
#  Print the uppercase letters in a string.
#

# Read input from the user.
inputStr = input("Enter a string: ")

# Find and display the uppercase letters.
for ch in inputStr :
   if ch == ch.upper :
      print(ch, end="")

print()

There’s a similar with digits. And I wonder if I can use .isdigit. This is the solution provided:

##
#  Count the number of digits in a string.
#

# Read input from the user.
inputStr = input("Enter a string: ")

# Check each character.  If it is a digit, add it to the count.
count = 0
for ch in inputStr :
   if ch >= "0" and ch <= "9" :
      count = count + 1

print("The string contains", count, "digits.")

Thanks!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.