Strings in python

Asked

Viewed 402 times

0

I know the basics of coding strings in Python, but I still cannot go forward and complete the whole requirement of the statement. I don’t know how to encode the quantities you require in the question.

Following statement:

Read keyboard strings until an empty string is read. Write on screen :

  • (A) the number of vowels read;

  • (B) the number of digits read;

  • (C) which string has the longest length read. If there is a tie, write one of them;
  • (D) the amount of palindrome strings read.

Definition:

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

For example:

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

  • 1

    len(minha_string) gives you the size of the string. Still your question is vague. Try to give more concrete examples of what you want, preferably with some code you’ve already written

  • M.xy, I saw that you completely updated the question, but you still didn’t post any code. Even with the current answers you couldn’t understand even the least to try? If so, please describe your doubts in the question.

2 answers

2

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

x = 'algo'

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

We can assume that the reader initially will not know that the object x will be responsible for receiving the user’s text. Right on the first line x receives a constant value. When reading, it is natural to think something like "x receives this value because somewhere will need to use it" (which is already a misinterpretation of the code, but the reader would not know it yet). On the next line, there is the repeat loop with the condition x != '' and obviously 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 user input, which code will make sense.

Since there is no way to predict how many times the user will enter with a different empty text, the correct 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 be:

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

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

-2


Hello. I’m going to make the simplest example possible, because I think it makes it easier to understand. Then it’s up to you to step up, take tests and learn more.

How to encode string reading from keyboard until an empty string be read?

x = 'algo'

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

How to write the amounts of letters in a string?

string = "minha string"

print(len(string))

or still, 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 pretty lost with language. Don’t worry, this is normal and everyone has to start from 0 one day. I suggest you attend the Guanabara classes where he teaches Python from the beginning. Are excellent.

  • pq do not use print(Len(string))

  • @Full explanation of why to use the .Len you find in this question https://answall.com/questions/201701/afinal-para-que-serve-a-fun%C3%A7%C3%A3o-Repr-no-python/201751#201751, but in short, using your method or my method will give in the same since, internally, Python will interpret with the .Len for this is the built-in method

  • 1

    There really is no need to use the method __len__. Or rather, it even harms the code when compared to the function len because it impairs readability. And you don’t need the range to iterate over string, they are iterable by themselves naturally.

  • How do you iterate directly with her? I didn’t know this, but I’ll change the code

  • 1

    The correct is to use the function len - the idea is that Python will always call the appropriate magic methods of a class when they exist. So next instead of .__next__, str instead of __str__,use the operator in instead of calling __contains__, etc..

  • 1

    I attend the classes of Guanabara yes and I’m enjoying it... but I’m still organizing the studies to advance further. I appreciate the comments. Helped me a lot.

  • I hope these classes don’t encourage the use of __len__ and similar

Show 2 more comments

Browser other questions tagged

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