+ 1
Can anyone explain ( for x in str) doing it mean running in background in this program ?
4 Réponses
+ 5
Try not to use str, as its used in python to change data to a string.
Anyways, you created the string called: str
a for statement is used to go through elements. If you think of strings as a list of characters and not just one group of letters it becomes easier.
thats also why correct variable names should be used:
for x in str:
...
# bad ^^^
# better vvv
for letter in str:
...
It literally just goes through every character in the array of characters you call str.
+ 3
This gives the same result without loops or variables
print("o ooo oooooo o o o o o o o o o o".count("o"))
+ 3
Jan Markus
Because count() is not a built-in function. It is a method on the str type.
https://docs.python.org/3/library/stdtypes.html#str.count
+ 3
Jan Markus , XXX is right. The count method also works on lists e.g. print([1, 2, 2, 3].count(2)) and tuples, but not dictionaries or generators.