How does Python know that that character is called a letter?
The line is saying.
when I replaced the letter with for bola in frase: and the code continued to work
And why would it be different?
You are free to name any variable as you wish. It is advised to use a meaningful name, but you can write any valid name for a code identifier following the syntax rules defined for Python.
In fact your original code already shows this, you used i
and it also worked, even though your code did a lot of unnecessary stuff. You tried to reproduce the for
gross of other languages, which is rarely necessary. The for
, also known as for each, is usually more suitable in most situations because it is used to "scan" a collection of data, it is rare that you need to use it as a counter, as was used in your reply in the mentioned forum. The for
aims to pick up a collection and go throwing an element of it into a variable to be used in each step, much simpler and less chance to do something wrong.
To remedy the question posed below, if by chance the interpretation is other than the question, and nothing in the question indicates anything else, and if so the question is not clear, I will ask more about.
The for
, at least in Python, it is a project pattern, like many others, that exist in the syntax of the language to facilitate the use of something very common avoiding errors, as I said before, and as it is a variable and other mechanisms.
That one Pattern design exists because it is common to need to go through a collection of data. In Python the most known collection is the list, another is the dictionary. But the most used is the collection string, which is nothing more than a list of characters. As it is so used it has a literal that lets you create a value with each character being written together without separators, only delimited by quotation marks (single or double).
Every data collection has some mechanisms to manipulate its content. One of them is the method __iter__()
. It provides an object that controls access to the data list, and with this object you can do some things, among them the most important is to go to the next item, with the __next__()
.
So if you are going to create a type that is a collection it is your obligation to create these methods in this type for it to properly process the iteration.
Your code actually looks like writing:
tmp_iter = frase.__iter__()
while True:
try:
letra = tmp_iter.__next__()
print(letra, end = "")
sleep(0.5)
except StopIteration:
break
I put in the Github for future reference.
What is said in passing is not a very efficient way, but Python does not have this commitment.
Obviously there is simplification there and there are several other ways to deal with it and different methods in the process.
So basically he interpreted the string as a list?
– Cadu
One string is a list of characters. The translation of the word is string, which can be interpreted also as set, collection, sequence, something being put one after another.
– Maniero
I think his doubt was more related to finding that letter was a special member or function of the phrase object.
– cfelipe
@Philip my interpretation is not this, and as the acceptance was given in this answer it seems to me that he agreed with my interpretation. Too bad I get a negative for a right answer. If I use the cri Erio of the interpretation of what the question is, I could negate yours for having interpreted it differently than I will but obviously I won’t do it because it would be voting for wrong reasons, as you did.
– Maniero
Fair. I think who has to solve this is who asked too, I changed my vote.
– cfelipe