{"id":"for-loops-strings","slug":"for-strings","title":"Going through a string","text":"A `for` loop goes through a string too, one character at a time:\n\n```python\nword = input(\"Word: \")\nfor char in word:\n    print(char)\n```\n\n```input\nHello\n```\n\n```output\nWord: Hello\nH\ne\nl\nl\no\n```\n\nSpaces and punctuation are characters as well, so the loop sees them too. `char` is just a variable name; `letter` or `c` would work as well.\n\n### Counting characters\n\nWith a helper variable, the loop can count characters of some kind. Here `in` checks whether a character is one of the vowels in the string `\"aeiou\"`:\n\n```python\ntext = \"Programming is fun\"\nvowels = 0\nfor char in text.lower():\n    if char in \"aeiou\":\n        vowels += 1\nprint(\"Vowels:\", vowels)\n```\n\n```output\nVowels: 5\n```\n\nThe loop goes through `text.lower()`, a copy of the text in small letters, so a capital `A` would be counted as well.\n\n### Building a new string\n\nA loop can also build a new string, character by character. Start with the empty string `\"\"` and add to it with `+=`:\n\n```python\ntext = \"a quiet, calm day\"\nresult = \"\"\nfor char in text:\n    if char == \" \":\n        result += \"_\"\n    else:\n        result += char\nprint(result)\n```\n\n```output\na_quiet,_calm_day\n```\n\nThe original string doesn't change: `result` is a new string, with an underscore wherever `text` has a space.\n\nThe same loops work in functions. A function that gets a string as a parameter goes through it in just the same way, and returns the count or the new string at the end:\n\n```python\ndef count_char(text: str, wanted: str) -> int:\n    count = 0\n    for char in text:\n        if char == wanted:\n            count += 1\n    return count\n\n\nprint(count_char(\"banana\", \"a\"))\nprint(count_char(\"banana\", \"x\"))\n```\n\n```output\n3\n0\n```\n","task":"Fill in the blanks so the program counts how many times the letter `a` is in the text the user types, as a small or a capital letter:\n\n```\nText: Banana Split\nLetter a: 3\n```\n","kind":"fill","seed":"text = input(\"Text: \")\ncount = 0\nfor char in [[text]]:\n    if char.[[lower]]() == \"a\":\n        count += 1\nprint(\"Letter a:\", count)\n","solution":"text = input(\"Text: \")\ncount = 0\nfor char in text:\n    if char.lower() == \"a\":\n        count += 1\nprint(\"Letter a:\", count)\n","hints":["The loop goes through the string that the user typed, in the variable `text`.","To count a capital `A` too, compare the character in small letters: `char.lower()`."],"tests":[{"name":"Test 1","stdin":"Banana Split\n","expect":"Text: Banana Split\nLetter a: 3","hidden":false},{"name":"Test 2","stdin":"Ada and ANNA ate apples\n","expect":"Text: Ada and ANNA ate apples\nLetter a: 7","hidden":true},{"name":"Test 3","stdin":"Python\n","expect":"Text: Python\nLetter a: 0","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write the function `hide_digits(text: str) -> str`, which returns a copy of the text where every digit (0 to 9) is replaced with `#`. Everything else stays as it is. For example, `hide_digits(\"Call 555-0123\")` returns `'Call ###-####'`.\n","kind":"code","seed":"def hide_digits(text: str) -> str:\n    pass\n\n\nif __name__ == \"__main__\":\n    print(hide_digits(\"Call 555-0123\"))\n","solution":"def hide_digits(text: str) -> str:\n    result = \"\"\n    for char in text:\n        if char in \"0123456789\":\n            result += \"#\"\n        else:\n            result += char\n    return result\n\n\nif __name__ == \"__main__\":\n    print(hide_digits(\"Call 555-0123\"))\n","hints":["Build a new string: start with `result = \"\"` and go through `text` with a `for` loop.","`char in \"0123456789\"` is true for a digit. Then add `\"#\"` to `result`, and otherwise the character itself.","Return `result` after the loop."],"tests":[{"name":"hide_digits(\"Call 555-0123\")","stdin":"","call":"hide_digits(\"Call 555-0123\")\n","expect":"'Call ###-####'","hidden":false,"returns":true},{"name":"hide_digits(\"no digits here\")","stdin":"","call":"hide_digits(\"no digits here\")\n","expect":"'no digits here'","hidden":true,"returns":true},{"name":"hide_digits(\"PIN 2580, code 7\")","stdin":"","call":"hide_digits(\"PIN 2580, code 7\")\n","expect":"'PIN ####, code #'","hidden":true,"returns":true}],"require":[],"forbid":[]},{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"word = \"stressed\"\nresult = \"\"\nfor char in word:\n    result = char + result\nprint(result)\ncount = 0\nfor char in \"mississippi\":\n    if char == \"s\":\n        count += 1\nprint(count)\n","solution":"word = \"stressed\"\nresult = \"\"\nfor char in word:\n    result = char + result\nprint(result)\ncount = 0\nfor char in \"mississippi\":\n    if char == \"s\":\n        count += 1\nprint(count)\n","hints":["In the first loop, each character is added to the front of `result`, not to its end.","Adding each character to the front turns the word around.","The second loop counts the letter s in mississippi."],"tests":[{"name":"Output","stdin":"","expect":"desserts\n4","hidden":false}],"require":[],"forbid":[],"lines":["desserts","4"]}]}