{"id":"strings-operations","slug":"operations","title":"Joining and repeating strings","text":"You've joined strings with `+` already. Python calls this **concatenation**: the strings are put one after the other, exactly as they are. Nothing is added between them, so any space has to be inside one of the strings:\n\n```python\nfirst = \"sun\"\nsecond = \"flower\"\nprint(first + second)\nprint(first + \" \" + second)\n```\n\n```output\nsunflower\nsun flower\n```\n\n### Repeating a string with *\n\nThe `*` operator works on strings too, when the other side is a whole number: it repeats the string that many times.\n\n```python\nprint(\"ha\" * 3)\nprint(\"-\" * 20)\nprint(\"=\" * 5 + \" Menu \" + \"=\" * 5)\n```\n\n```output\nhahaha\n--------------------\n===== Menu =====\n```\n\nAs with numbers, `*` comes before `+`, so on the last line Python first repeats the two `=` strings and then joins the three pieces.\n\nThe number can be a variable, which makes `*` handy in loops. This loop draws a triangle, one `#` wider in every round:\n\n```python\nwidth = 1\nwhile width <= 4:\n    print(\"#\" * width)\n    width += 1\n```\n\n```output\n#\n##\n###\n####\n```\n\n### Numbers from input\n\nA string can only be repeated a whole number of times. `input` always gives a string, even when the user types digits, so forgetting `int` stops the program:\n\n```python\nwidth = input(\"Width: \")\nprint(\"*\" * width)\n```\n\n```input\n6\n```\n\n```output\nWidth: 6\n```\n\n```crash\nTraceback (most recent call last):\n  File \"main.py\", line 2, in <module>\n    print(\"*\" * width)\n          ~~~~^~~~~~~\nTypeError: can't multiply sequence by non-int of type 'str'\n```\n\nA **sequence** is Python's word for a value that holds things in order, as a string holds its characters. The message says that `*` got a string on its other side (`'str'`) where it needs an `int`. With `width = int(input(\"Width: \"))` the program works.\n","task":"Fill in the blank so the program prints a line of 12 `=` characters under the title, as long as the title itself.\n\n```\nPython Arena\n============\n```\n","kind":"fill","seed":"print(\"Python Arena\")\nprint(\"=\" [[*]] 12)\n","solution":"print(\"Python Arena\")\nprint(\"=\" * 12)\n","hints":["A string and a whole number can be put together with one operator: it repeats the string.","Repeating is `*`, as in `\"-\" * 20`."],"tests":[{"name":"Output","stdin":"","expect":"Python Arena\n============","hidden":false}],"require":[],"forbid":[],"more":[{"task":"What does this program print? Type each line it prints.\n","kind":"predict","seed":"print(\"ab\" * 3)\nprint(\"-\" * 4 + \">\")\nprint(2 * \"xy\" + \"z\")\nprint(\"3\" * 3)\n","solution":"print(\"ab\" * 3)\nprint(\"-\" * 4 + \">\")\nprint(2 * \"xy\" + \"z\")\nprint(\"3\" * 3)\n","hints":["`*` with a string and a whole number repeats the string. The number may also come first: `2 * \"xy\"` is the same as `\"xy\" * 2`.","`*` comes before `+`: the repeated string is made first, then the other string is joined to its end.","`\"3\"` is in quotes, so it's a string, not a number. It's repeated, not multiplied."],"tests":[{"name":"Output","stdin":"","expect":"ababab\n---->\nxyxyz\n333","hidden":false}],"require":[],"forbid":[],"lines":["ababab","---->","xyxyz","333"]},{"task":"Write a program that asks for a width and a height and draws a box. The top and the bottom line are a `+`, then as many `-` characters as the width, then another `+`. Between them come as many lines as the height, each one a `|`, as many spaces as the width, and another `|`.\n\n```\nWidth: 5\nHeight: 2\n+-----+\n|     |\n|     |\n+-----+\n```\n","kind":"code","seed":"width = int(input(\"Width: \"))\nheight = int(input(\"Height: \"))\n","solution":"width = int(input(\"Width: \"))\nheight = int(input(\"Height: \"))\nprint(\"+\" + \"-\" * width + \"+\")\nrow = 0\nwhile row < height:\n    print(\"|\" + \" \" * width + \"|\")\n    row += 1\nprint(\"+\" + \"-\" * width + \"+\")\n","hints":["Build the top line from three pieces: `\"+\"`, then `\"-\"` repeated `width` times, then `\"+\"`. The bottom line is the same.","The lines in between all look the same, so print them in a loop that runs `height` times.","A middle line is `\"|\" + \" \" * width + \"|\"`."],"tests":[{"name":"Test 1","stdin":"5\n2\n","expect":"Width: 5\nHeight: 2\n+-----+\n|     |\n|     |\n+-----+","hidden":false},{"name":"Test 2","stdin":"1\n1\n","expect":"Width: 1\nHeight: 1\n+-+\n| |\n+-+","hidden":true},{"name":"Test 3","stdin":"8\n0\n","expect":"Width: 8\nHeight: 0\n+--------+\n+--------+","hidden":true},{"name":"Test 4","stdin":"3\n4\n","expect":"Width: 3\nHeight: 4\n+---+\n|   |\n|   |\n|   |\n|   |\n+---+","hidden":true}],"require":[],"forbid":[]}]}