+ 1

Can anyone explain ( for x in str) doing it mean running in background in this program ?

https://code.sololearn.com/cR6vUKaplg5E/?ref=app

21st Apr 2021, 8:44 AM
Andy
Andy - avatar
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.
21st Apr 2021, 8:52 AM
Slick
Slick - avatar
+ 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"))
21st Apr 2021, 11:11 AM
David Ashton
David Ashton - avatar
+ 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
21st Apr 2021, 11:54 AM
XXX
XXX - avatar
+ 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.
21st Apr 2021, 6:37 PM
David Ashton
David Ashton - avatar