Strings in python [closed]

0

I know the basic encoding of strings in Python, but I still can not move forward and complete the whole requirement of the statement. I do not know how to code the quantities you require in the question.

Follow the sentence:

Read keyboard strings until an empty string is read. Type in screen:

  • (A) the number of vowels read;

  • (B) the number of digits read;

  • (C) which was the longest string read. If there is a tie, write one;
  • (D) the amount of palindromic strings read.

Definition:

A string is said to be palíndrome if and only if, when read from first to last character is equal to string read from last to first character.

For example:

“AMA” , “POP” , “SOCORRAMMESUBINOONIBUSEMMARROCOS” , “” , and “Z” are palindromes.

    
asked by anonymous 06.03.2018 / 15:11

2 answers

-1

Hello. I will make the example as simple as possible, because I think it is easier to understand. Then it's up to you to increment, test, and learn more.

  

How to code keyboard string reading until an empty string   be read?

x = 'algo'

while x != '':
    x = input()
  

How to write on the screen the amounts of letters of a string?

string = "minha string"

print(len(string))

or, if you prefer loop:

string = "minha string"
cont = 0

for x in range(string.__len__()):
    if string[x] != 0:
        cont +=1

print('Minha string tem {} caracteres' .format(cont))

Addendum: I saw in another question that you are quite lost with language. Do not worry, this is normal and everyone has to start at 0 a day. I suggest you watch Guanabara classes where he teaches Python from the beginning . They are excellent.

    
06.03.2018 / 16:02
2

If the intention, which was not clear in the question, is actually reading the user's multiple texts until the user does not enter something, you must make an infinite loop until the stop condition is satisfied. In a way, that's what the code in the other answer does, but in my opinion, it's not the best way to do it.

x = 'algo'

while x != '':
    x = input()

We can assume that the reader will not initially know that the x object will be responsible for receiving the user's text. Then in the first line x receives a constant value. When reading, it is natural to think of something like " x gets this value because somewhere it will need to use it" (which is already a misinterpretation of the code, but the reader would not know it yet). In the next line, there is the loop of repetition with the condition x != '' and of course the reader will think something like "but if x is constant, it will always be different from '' , then this loop will be infinite? which, again, will be a misinterpretation of the code. Only when reading the last line, where x receives the user input, that code will make sense.

Since there is no way to predict how many times the user will enter text other than empty, the correct thing is to create the infinite loop with while True ; then you read the user input and check, if it is an empty text, stop the loop. The code would look like:

while True:
    x = input("Digite algo:")
    if not x:
        break
    print("Tamanho do texto", len(x))

As commented, the len function was used to get the size of the text.

    
07.03.2018 / 17:02