1 00:00:00,940 --> 00:00:05,190 In our cat food store program, our printed question is running up against the space 2 00:00:05,190 --> 00:00:07,539 where the user types their answer. 3 00:00:07,539 --> 00:00:11,463 To fix this, we need to add a space on to the end of the user question. 4 00:00:11,463 --> 00:00:15,460 We can do this by taking the question string and concatenating or 5 00:00:15,460 --> 00:00:19,129 joining a string consisting of a single space on to the end of it. 6 00:00:25,990 --> 00:00:29,370 To concatenate two strings, you just type a plus sign between them. 7 00:00:31,010 --> 00:00:34,510 So this code here we'll join the strings A and B together and 8 00:00:34,510 --> 00:00:37,510 this would join the string some words and more words together. 9 00:00:37,510 --> 00:00:39,820 Let me save this and run it. 10 00:00:44,800 --> 00:00:46,120 And here's the result. 11 00:00:46,120 --> 00:00:50,230 You can see that the string some words is smashed together with more words. 12 00:00:52,370 --> 00:00:54,480 If we want a space between the joint strings, 13 00:00:54,480 --> 00:00:58,270 we'll need to add it ourselves by a concatenating a third string in there. 14 00:00:59,370 --> 00:01:01,130 Let me save this and re-run it. 15 00:01:04,320 --> 00:01:06,020 There we go, and now there's a space. 16 00:01:10,850 --> 00:01:13,780 We can also concatenate strings stored in variables. 17 00:01:13,780 --> 00:01:18,080 So let's declare a new variable named myString and storeString in it. 18 00:01:18,080 --> 00:01:20,739 And now, let's concatenate that together with another string. 19 00:01:23,226 --> 00:01:24,380 Let me save that. 20 00:01:27,100 --> 00:01:32,010 And let's try running it, there's the string from our variable, 21 00:01:32,010 --> 00:01:34,520 and here's the string we concatenated onto it. 22 00:01:42,210 --> 00:01:45,390 One important thing to note, though, is that simply concatenating onto 23 00:01:45,390 --> 00:01:49,400 a string stored in a variable doesn't change the value in the variable. 24 00:01:49,400 --> 00:01:52,490 So, if we were trying to print the content of my string again, 25 00:01:52,490 --> 00:01:53,470 let's try that real quick. 26 00:01:56,330 --> 00:02:00,130 You can see that my string still just contains the value we originally assigned 27 00:02:00,130 --> 00:02:01,060 to it, a string. 28 00:02:01,060 --> 00:02:02,750 There's no abc on the end. 29 00:02:08,210 --> 00:02:09,970 To update the value in the variable, 30 00:02:09,970 --> 00:02:13,610 we need to assign the concatenated value back to it. 31 00:02:13,610 --> 00:02:15,649 We can do that with the += operator. 32 00:02:15,649 --> 00:02:20,510 This will concatenate a new string onto the string currently contained 33 00:02:20,510 --> 00:02:22,690 in the myString variable. 34 00:02:22,690 --> 00:02:26,870 And then assign that new concatenated value back to myString. 35 00:02:26,870 --> 00:02:32,190 So let's try printing that updated value, save this and try running it. 36 00:02:34,060 --> 00:02:38,330 And now you can see that myString contains the updated value, a string abc. 37 00:02:41,710 --> 00:02:45,840 I can even concatenate additional strings onto the updated strings. 38 00:02:45,840 --> 00:02:47,310 So let's add def and 39 00:02:47,310 --> 00:02:52,790 assign that back to myString and then print the resulting value. 40 00:02:55,125 --> 00:02:59,000 And let's try running this, and you can see that, first, 41 00:02:59,000 --> 00:03:02,790 we updated myString so that it contained a string abc, and 42 00:03:02,790 --> 00:03:08,240 then we concatenated an additional string onto that, a string abc def. 43 00:03:08,240 --> 00:03:11,650 This is what the myString variable contains at the end of the program. 44 00:03:13,040 --> 00:03:16,460 So by assigning concatenated strings back to the same variable, 45 00:03:16,460 --> 00:03:18,850 you can keep building up longer and longer strings. 46 00:03:18,850 --> 00:03:23,360 Let's use string concatenation to fix our ask method. 47 00:03:23,360 --> 00:03:28,010 We'll simply take the value that we're passing to the write method, and 48 00:03:28,010 --> 00:03:30,500 concatenate a space onto the end of it. 49 00:03:31,730 --> 00:03:35,400 That will cause there to be a space between the question that gets asked and 50 00:03:35,400 --> 00:03:37,580 the answer the user types. 51 00:03:41,590 --> 00:03:43,980 So make sure to save that, and let's try running it. 52 00:03:45,980 --> 00:03:47,250 How many cans are you ordering? 53 00:03:47,250 --> 00:03:50,430 Let's say 55, and there's a space there now. 54 00:03:52,180 --> 00:03:54,900 Right now, we're simply printing the value the user enters so 55 00:03:54,900 --> 00:03:59,690 we can debug it, but it would be helpful for the user to know what they entered, so 56 00:03:59,690 --> 00:04:04,030 let's make this a little more user friendly and convert this into a sentence. 57 00:04:04,030 --> 00:04:06,780 We'll do that using string concatenation again. 58 00:04:06,780 --> 00:04:12,000 We'll say you entered, As a string, 59 00:04:12,000 --> 00:04:15,770 and then concatenate that together with the value in the entry variable. 60 00:04:15,770 --> 00:04:21,490 And concatenate that with cans to end the sentence. 61 00:04:21,490 --> 00:04:22,930 Save that let's try running it. 62 00:04:27,930 --> 00:04:32,090 And whoops it joins the value we entered together with the rest of the sentence but 63 00:04:32,090 --> 00:04:34,410 theres no spaces surrounding the value we entered. 64 00:04:36,060 --> 00:04:37,000 So let's fix that. 65 00:04:38,740 --> 00:04:44,420 We simply need to add spaces onto the end of the string we're adding before 66 00:04:44,420 --> 00:04:49,490 the entry and onto the start of the string we're adding after the entry. 67 00:04:49,490 --> 00:04:50,140 Let's save that. 68 00:04:52,780 --> 00:04:54,030 And try running it again. 69 00:04:56,250 --> 00:04:57,600 Okay, that looks much better.