{"id":"variables-combining","slug":"combining-values","title":"Combining values when printing","text":"A program often prints text and a number on the same line. Joining them with `+` seems natural, but it doesn't work:\n\n```python\nscore = 7 * 6\nprint(\"Your score: \" + score)\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 2, in <module>\n    print(\"Your score: \" + score)\n          ~~~~~~~~~~~~~~~^~~~~~~\nTypeError: can only concatenate str (not \"int\") to str\n```\n\nTo concatenate is to join strings. `+` can join a string to a string, but not an integer to a string. Python won't guess whether you meant to add or to join, so it stops.\n\n### Turning a number into a string with str\n\n`str` converts a value into a string: `str(42)` is the string `\"42\"`. Commands like `str` and `print`, which take a value in parentheses and do something with it, are called **functions**. And a string joins happily with another string:\n\n```python\nscore = 7 * 6\nprint(\"Your score: \" + str(score))\n```\n\n```output\nYour score: 42\n```\n\n### Commas\n\nThe other way is the one from the previous module: commas. `print` accepts values of any type separated by commas and puts a space between each two:\n\n```python\nscore = 7 * 6\nlevel = 3\nprint(\"Your score:\", score)\nprint(\"Level\", level, \"of\", 10)\n```\n\n```output\nYour score: 42\nLevel 3 of 10\n```\n\nThe automatic spaces are handy, but sometimes they get in the way. Then joining with `str` gives you full control:\n\n```python\nnumber = 42\nprint(\"Order #\", number)\nprint(\"Order #\" + str(number))\n```\n\n```output\nOrder # 42\nOrder #42\n```\n\n### Staying on the same line with end\n\nEvery `print` ends its line, so the next `print` starts on a new one. You can change that. Inside the parentheses, `end=\"\"` tells `print` to end with an empty string instead of a line break, so the next `print` carries on where this one stopped:\n\n```python\nprint(\"Loading\", end=\"\")\nprint(\"...\", end=\"\")\nprint(\" done\")\n```\n\n```output\nLoading... done\n```\n\nWhatever string you give to `end` is printed in place of the line break: with `end=\" \"`, the line ends with a space and the next `print` continues after it.\n","task":"Fill in the blank so the program prints the line below. `points` holds an integer, so it can't be joined to a string as it is.\n\n```\nPoints: 60\n```\n","kind":"fill","seed":"points = 15 * 4\nprint(\"Points: \" + [[str]](points))\n","solution":"points = 15 * 4\nprint(\"Points: \" + str(points))\n","hints":["`+` can only join a string to another string. Which function turns a number into a string?","`str(points)` is the string `\"60\"`."],"tests":[{"name":"Output","stdin":"","expect":"Points: 60","hidden":false}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"count = 3\nprint(\"I have\", count, \"cats\")\nprint(\"I have \" + str(count) + \" cats\")\nprint(count, \"!\")\nprint(str(count) + \"!\")\n","solution":"count = 3\nprint(\"I have\", count, \"cats\")\nprint(\"I have \" + str(count) + \" cats\")\nprint(count, \"!\")\nprint(str(count) + \"!\")\n","hints":["Commas in `print` put a space between the values, whatever their types.","`str(count)` is the string `3`, and `+` joins it with nothing in between."],"tests":[{"name":"Output","stdin":"","expect":"I have 3 cats\nI have 3 cats\n3 !\n3!","hidden":false}],"require":[],"forbid":[],"lines":["I have 3 cats","I have 3 cats","3 !","3!"]},{"task":"Change the program so that it prints the whole countdown on one line, with a space between the parts, as shown below. Keep the four `print` commands.\n\n```\n3... 2... 1... Go!\n```\n","kind":"code","seed":"print(\"3...\")\nprint(\"2...\")\nprint(\"1...\")\nprint(\"Go!\")\n","solution":"print(\"3...\", end=\" \")\nprint(\"2...\", end=\" \")\nprint(\"1...\", end=\" \")\nprint(\"Go!\")\n","hints":["Each `print` ends its line. Which extra setting changes what a `print` puts at the end?","Give the first three a space instead of the line break: `print(\"3...\", end=\" \")`.","The last `print` should end its line as usual, so it stays as it is."],"tests":[{"name":"Output","stdin":"","expect":"3... 2... 1... Go!","hidden":false}],"require":[{"pattern":"\\bprint\\s*\\(","min":4,"max":4,"message":"Keep the four `print` commands and change how they end."},{"pattern":"\\bend\\s*=","min":3,"message":"Use `end=` in the first three `print` commands."}],"forbid":[]}]}