Identifying words that begin capitalized in lists

Asked

Viewed 33 times

-2

I wanted to identify if the list item has the first uppercase letter.
I’ve tried For and While, but the result is always the same.

texto = input('Texto: ')
texto.split()
cont = 0
for c in texto:
    if cont != 0:
        a = c[0]
        if a.isupper:
            assunto = True
            a = c
            break
    cont += 1
if assunto:
    print(a)
else:
    print('Sem assunto')

The answer to the code is always the second letter of the first word, and I don’t know why.

Example: If the phrase is "Do you like Sushi?" the program will return the letter "O", which is in the word "You". Strangely, it only returns the letters that are in this place.

What’s wrong with the code?

1 answer

1

Let’s go in pieces

The variable texto will take an entrance.

texto = input('Texto: ')

This part divides text into words using space as delimiter, however would have to assign the result to a variable

texto.split()
frase = texto.split()

Now, just iterate and check the first letter of each word

for palavra in frase:
   if palavra[0].isupper():
      # faça o que quiser

Note isupper() is a method and should be called as such

I hope I’ve helped.

  • Thank you very much, my problem has been solved and it will be quite useful to me

  • I discovered the problem. isupper() returns True to spaces

Browser other questions tagged

You are not signed in. Login or sign up in order to post.