Error making python formatting using . find command

Asked

Viewed 39 times

-1

I’m trying to find how many times the letter "A" repeats in a variable, but I can’t find the error when giving the format command.

p = str(input('Digite uma frase: ')).strip().upper() 

print(f' {(p.find('A'))}')

SyntaxError: f-string: unmatched '('

1 answer

0


The problem is that you are trying to use simple quotes inside a string created with simple quotes. The result is an unintentional closure of the string - leaving out the letter A of it - and opening a new string in the sequence.

To solve the problem, just use double quotes to create the string, as in the code below:

print(f" {(p.find('A'))}")

In addition, the method find() is only used to get the position of a substring. To count how many times a substring appears in the string, use the method count(), thus:

print(f" {p.count('A')}")

Browser other questions tagged

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