{"id":"strings-length","slug":"length","title":"The length of a string","text":"You met the function `len` (short for length) in part 2: it tells how many characters a string has. Every character counts: letters, digits, spaces and punctuation alike. This module puts it to work.\n\n```python\nprint(len(\"hello\"))\nprint(len(\"hello there!\"))\nprint(len(\"\"))\n```\n\n```output\n5\n12\n0\n```\n\nThe empty string `\"\"` has no characters, so its length is 0. Note that `len` counts characters, not words: `\"hello there!\"` is two words but 12 characters, the space and the `!` included.\n\n`len` gives back a whole number, so you can use it wherever a number fits. In a condition, `len(name) < 3` is `True` for a name of fewer than three characters, and `len(first) > len(second)` compares two strings by their length. In a calculation, `10 - len(word)` tells how many characters a word lacks from 10. And it can be the number of times to repeat a string. This program underlines whatever the user types:\n\n```python\ntitle = input(\"Title: \")\nprint(title)\nprint(\"-\" * len(title))\n```\n\n```input\nLoops and strings\n```\n\n```output\nTitle: Loops and strings\nLoops and strings\n-----------------\n```\n\n### Lining up text\n\nThe length is also what you need to line text up. When you add numbers on paper, you write them in a column lined up on the right, ones under ones and tens under tens. A program can do the same: to put a number at the right edge of a column 6 characters wide, print `6 - len(number)` spaces in front of it. Here the numbers are strings, since `len` counts characters; `int` turns them into numbers for the sum, and `str` turns the sum back into a string:\n\n```python\nfirst = \"385\"\nsecond = \"1250\"\ntotal = str(int(first) + int(second))\nprint(\" \" * (6 - len(first)) + first)\nprint(\" \" * (6 - len(second)) + second)\nprint(\"-\" * 6)\nprint(\" \" * (6 - len(total)) + total)\n```\n\n```output\n   385\n  1250\n------\n  1635\n```\n\nThe parentheses matter. Without them, `\" \" * 6 - len(first)` would first make a string of 6 spaces and then try to subtract a number from it, which is an error.\n\n### Numbers have no length\n\n`len` counts the characters of a string. A number isn't a string, so it has no length:\n\n```python\nyear = 2026\nprint(len(year))\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 2, in <module>\n    print(len(year))\n          ~~~^^^^^^\nTypeError: object of type 'int' has no len()\n```\n\nTo count the digits of a number, make it a string first: `len(str(year))` is 4.\n","task":"Fill in the blank so the program underlines the heading typed in with `=` characters, exactly as long as the heading.\n\n```\nHeading: Chapter one\nChapter one\n===========\n```\n","kind":"fill","seed":"heading = input(\"Heading: \")\nprint(heading)\nprint(\"=\" * [[len(heading)]])\n","solution":"heading = input(\"Heading: \")\nprint(heading)\nprint(\"=\" * len(heading))\n","hints":["The line must have as many `=` characters as the heading has characters.","`len(...)` gives the number of characters of the string in its parentheses."],"tests":[{"name":"Test 1","stdin":"Chapter one\n","expect":"Heading: Chapter one\nChapter one\n===========","hidden":false},{"name":"Test 2","stdin":"Hi\n","expect":"Heading: Hi\nHi\n==","hidden":true},{"name":"Test 3","stdin":"A much longer heading\n","expect":"Heading: A much longer heading\nA much longer heading\n=====================","hidden":true}],"require":[],"forbid":[],"more":[{"task":"Write a program that asks for a password with the prompt `Password: `. If it has fewer than 8 characters, the program says how many characters are missing, as below. Otherwise it prints `Length OK`.\n\n```\nPassword: secret\nToo short, add 2 more\n```\n","kind":"code","seed":"password = input(\"Password: \")\n","solution":"password = input(\"Password: \")\nif len(password) < 8:\n    missing = 8 - len(password)\n    print(f\"Too short, add {missing} more\")\nelse:\n    print(\"Length OK\")\n","hints":["`len(password)` is the number of characters typed in. Compare it with 8 in an `if`.","The number of missing characters is 8 minus the length."],"tests":[{"name":"Test 1","stdin":"secret\n","expect":"Password: secret\nToo short, add 2 more","hidden":false},{"name":"Test 2","stdin":"correct horse\n","expect":"Password: correct horse\nLength OK","hidden":true},{"name":"Test 3","stdin":"abcdefgh\n","expect":"Password: abcdefgh\nLength OK","hidden":true},{"name":"Test 4","stdin":"x\n","expect":"Password: x\nToo short, add 7 more","hidden":true}],"require":[],"forbid":[]},{"task":"Write a program that asks for a dish and its price and prints them on one line exactly 24 characters wide, with dots filling the space between the dish and the price. Read the price as text: it doesn't need to be a number here.\n\n```\nDish: Pasta\nPrice: 12\nPasta.................12\n```\n","kind":"code","seed":"dish = input(\"Dish: \")\nprice = input(\"Price: \")\n","solution":"dish = input(\"Dish: \")\nprice = input(\"Price: \")\ndots = 24 - len(dish) - len(price)\nprint(dish + \".\" * dots + price)\n","hints":["The dots fill what's left of the 24 characters after the dish and the price.","The number of dots is `24 - len(dish) - len(price)`. Keep it in a variable.","Join the three pieces with `+`: the dish, `\".\"` repeated that many times, and the price."],"tests":[{"name":"Test 1","stdin":"Pasta\n12\n","expect":"Dish: Pasta\nPrice: 12\nPasta.................12","hidden":false},{"name":"Test 2","stdin":"Soup of the day\n7\n","expect":"Dish: Soup of the day\nPrice: 7\nSoup of the day........7","hidden":true},{"name":"Test 3","stdin":"Tea\n3\n","expect":"Dish: Tea\nPrice: 3\nTea....................3","hidden":true},{"name":"Test 4","stdin":"Fish and chips\n15\n","expect":"Dish: Fish and chips\nPrice: 15\nFish and chips........15","hidden":true}],"require":[],"forbid":[]}]}