for
is used to loop through the defined variable s
, which in this case is a string containing letters, digits (numbers), and whitespace (spaces, tabs etc.).
First time:
' Abs3 asdasd asd11 111 11ss'
^
As we can see, the first character of the string is: “” (space).
The code goes on to test if “” is a letter (
isalpha
) or if it is a digit (isdigit
). In this first case, it is neither and the for
loop continues with the next character:
' Abs3 asdasd asd11 111 11ss'
^
Second character is “A
“. In this case, the if statement results in alpha_count += 1
. In other words: The string contains 1 letter.
Third time the for
is looped:
' Abs3 asdasd asd11 111 11ss'
^
… is a letter. Thus: alpha_count += 1
= 2
.
The for statement loops through the whole string before revealing that the string contains: 14 letters and 8 digits.
solved What is the use of for in python [closed]