{"id":"input-joining","slug":"joining","title":"Joining strings with +","text":"A greeting and the user's name usually belong on the same line. To build that line, join strings with `+`. Putting strings end to end like this is called **concatenation**:\n\n```python\nname = input(\"Your name: \")\nprint(\"Welcome, \" + name + \"!\")\n```\n\n```input\nAda\n```\n\n```output\nYour name: Ada\nWelcome, Ada!\n```\n\nPython first builds one string out of the three parts: `\"Welcome, \"`, the value of `name` and `\"!\"`. Then `print` prints that string. You can join as many parts as you need, and you can use the same variable several times in one line.\n\n### Spaces are your job\n\n`+` puts nothing between the strings it joins, not even a space. Any space you want must be inside the quotes:\n\n```python\nanimal = input(\"Animal: \")\nprint(\"The\" + animal + \"is asleep.\")\nprint(\"The \" + animal + \" is asleep.\")\n```\n\n```input\ncat\n```\n\n```output\nAnimal: cat\nThecatis asleep.\nThe cat is asleep.\n```\n\nIn the second line, the space after `The` and the space before `is` are part of the strings, so the words come out apart.\n\n### Storing a joined string\n\nThe result of `+` is a new string, and you can store it in a variable like any other value:\n\n```python\nword = input(\"Word: \")\nloud = word + \"!!!\"\nprint(loud)\nprint(loud + \" \" + loud)\n```\n\n```input\nyes\n```\n\n```output\nWord: yes\nyes!!!\nyes!!! yes!!!\n```\n\n### A missing +\n\nEvery part needs a `+` between it and the next one. Without it, Python can't read the line at all:\n\n```python\nname = input(\"Your name: \")\nprint(\"Hi \" name)\n```\n\n```crash\n  File \"main.py\", line 2\n    print(\"Hi \" name)\n          ^^^^^^^^^^\nSyntaxError: invalid syntax. Perhaps you forgot a comma?\n```\n\nThis is a syntax error, so nothing runs, not even the `input` on line 1. Python guesses a missing comma. Commas do work in `print`, as the next step shows, but here the missing piece is a `+`.\n","task":"Fill in the blanks so the program greets the user by name. If the user types `Grace`, the run looks like this:\n\n```\nName: Grace\nGood morning, Grace.\n```\n","kind":"fill","seed":"name = input(\"Name: \")\nprint(\"Good morning, \" [[+]] name [[+]] \".\")\n","solution":"name = input(\"Name: \")\nprint(\"Good morning, \" + name + \".\")\n","hints":["The line joins three strings into one. What joins strings in Python?","Put `+` in both blanks: `\"Good morning, \" + name + \".\"`."],"tests":[{"name":"Test 1","stdin":"Grace\n","expect":"Name: Grace\nGood morning, Grace.","hidden":false},{"name":"Test 2","stdin":"Linus\n","expect":"Name: Linus\nGood morning, Linus.","hidden":true}],"require":[],"forbid":[],"more":[{"task":"The smoothie bar's program glues the words together. Add the missing spaces, so that a run looks like this:\n\n```\nFruit: mango\nOne mango smoothie, coming up!\n```\n","kind":"code","seed":"fruit = input(\"Fruit: \")\nprint(\"One\" + fruit + \"smoothie, coming up!\")\n","solution":"fruit = input(\"Fruit: \")\nprint(\"One \" + fruit + \" smoothie, coming up!\")\n","hints":["Run it and look at the output. Where are spaces missing?","The spaces go inside the quotes: one after `One` and one before `smoothie`."],"tests":[{"name":"Test 1","stdin":"mango\n","expect":"Fruit: mango\nOne mango smoothie, coming up!","hidden":false},{"name":"Test 2","stdin":"banana\n","expect":"Fruit: banana\nOne banana smoothie, coming up!","hidden":true}],"require":[],"forbid":[]},{"task":"Write a program that asks for a user name with the prompt `User name: ` and prints the user's email address at `example.com`. If the user types `ada`, the run looks like this:\n\n```\nUser name: ada\nYour address is ada@example.com\n```\n","kind":"code","seed":"# Ask for the user name, then print the address.\n","solution":"# Ask for the user name, then print the address.\nuser = input(\"User name: \")\nprint(\"Your address is \" + user + \"@example.com\")\n","hints":["Start by reading the user name into a variable with `input`.","The output line has three parts: the text `Your address is ` (with a space at the end), the user name, and `@example.com`.","Join them with `+`: `print(\"Your address is \" + user + \"@example.com\")`, if your variable is called `user`."],"tests":[{"name":"Test 1","stdin":"ada\n","expect":"User name: ada\nYour address is ada@example.com","hidden":false},{"name":"Test 2","stdin":"grace_h\n","expect":"User name: grace_h\nYour address is grace_h@example.com","hidden":true}],"require":[],"forbid":[]}]}