1 00:00:00,000 --> 00:00:04,186 Previously, I introduced you how to dynamically insert 2 00:00:04,187 --> 00:00:07,210 the value of a variable inside a string. 3 00:00:07,211 --> 00:00:10,996 So here we constructed Hello and whatever value 4 00:00:10,997 --> 00:00:14,400 the user entered in the input function. 5 00:00:14,930 --> 00:00:18,308 So this worked with Python 2 and 3. 6 00:00:18,309 --> 00:00:23,274 This works with Python 3.6 and later versions. 7 00:00:23,275 --> 00:00:25,098 Now, what if we want to insert 8 00:00:25,099 --> 00:00:27,042 more than one value inside a string? 9 00:00:27,043 --> 00:00:28,156 How to do that? 10 00:00:28,157 --> 00:00:31,932 For example, let's say the first input function 11 00:00:31,933 --> 00:00:33,324 will ask us for the name, and the 12 00:00:33,325 --> 00:00:35,510 second will ask us for the surname. 13 00:00:37,130 --> 00:00:41,850 Let's call this variable name and this one surname. 14 00:00:42,430 --> 00:00:45,888 To use this version of string formatting with 15 00:00:45,889 --> 00:00:48,000 two variables, you'd need to do this. 16 00:00:48,001 --> 00:00:51,482 Instead of just one variable, you need to use round 17 00:00:51,483 --> 00:00:55,268 brackets and then enter all the variables you want to 18 00:00:55,269 --> 00:01:00,404 insert in the string name and surname in this case. 19 00:01:00,405 --> 00:01:03,550 Now, if you execute this, you're going to get an error. 20 00:01:03,551 --> 00:01:05,464 So you need to do something more there. 21 00:01:05,465 --> 00:01:10,766 Let's say Ardit Sulce, and you got a TypeError. 22 00:01:10,767 --> 00:01:14,632 Not all arguments converted during string formatting, which 23 00:01:14,633 --> 00:01:19,170 means that the arguments are name and surname. 24 00:01:19,171 --> 00:01:21,900 Not all of them were converted because 25 00:01:21,901 --> 00:01:24,172 only name was converted because you only 26 00:01:24,173 --> 00:01:27,228 had one placeholder here, percentage s. 27 00:01:27,229 --> 00:01:30,940 You want to have two placeholders, one for surname too. 28 00:01:31,950 --> 00:01:37,398 If you execute that well, we get another error. 29 00:01:37,399 --> 00:01:41,364 But that's not related to this line here. 30 00:01:41,365 --> 00:01:43,316 This line is perfectly fine. 31 00:01:43,317 --> 00:01:44,404 It works. 32 00:01:44,405 --> 00:01:45,892 Why does it work? 33 00:01:45,893 --> 00:01:49,492 Well, because you can see that the error happened. 34 00:01:49,493 --> 00:01:52,298 So this is where the trace back starts. 35 00:01:52,299 --> 00:01:56,392 You see that the error happens in file basics.py, which 36 00:01:56,393 --> 00:02:00,088 is the file where we have the code in line 5, 37 00:02:00,089 --> 00:02:04,360 which means that above line 5, everything is okay. 38 00:02:04,361 --> 00:02:08,306 So this line now has passed the error checking. 39 00:02:08,307 --> 00:02:09,910 It's free of errors. 40 00:02:10,650 --> 00:02:12,028 So this is the line that has 41 00:02:12,029 --> 00:02:15,122 the error, and it's a NameError. 42 00:02:15,123 --> 00:02:17,586 name user_input is not defined. 43 00:02:17,587 --> 00:02:19,692 So this user_input here has 44 00:02:19,693 --> 00:02:22,416 not been defined in our code. 45 00:02:22,417 --> 00:02:25,024 We don't have a variable with that name. 46 00:02:25,025 --> 00:02:26,288 We're just using it here. 47 00:02:26,289 --> 00:02:27,680 But we didn't define it. 48 00:02:27,681 --> 00:02:28,902 And here is a trick. 49 00:02:28,903 --> 00:02:31,076 If you don't want to execute that 50 00:02:31,077 --> 00:02:33,956 line, you can comment it out. 51 00:02:33,957 --> 00:02:37,890 So by adding this symbol here, 52 00:02:37,891 --> 00:02:40,036 Python will ignore this line. 53 00:02:40,037 --> 00:02:42,002 It will treat it as a comment, 54 00:02:42,003 --> 00:02:44,100 [Author typing] 55 00:02:44,101 --> 00:02:45,704 not as code. 56 00:02:45,705 --> 00:02:47,230 So save the script. 57 00:02:47,231 --> 00:02:51,730 Execute, enter your input as a user. 58 00:02:52,390 --> 00:02:55,460 And this is the output we get this time. 59 00:02:56,970 --> 00:03:00,562 So that was a good exercise on how to fix errors. 60 00:03:00,563 --> 00:03:03,762 Now back to our string formatting operations. 61 00:03:03,763 --> 00:03:07,794 Let's see how this now will work with two variables. 62 00:03:07,795 --> 00:03:10,838 So, user_input is not a variable anymore. 63 00:03:10,839 --> 00:03:12,768 We want to use name. 64 00:03:12,769 --> 00:03:14,192 So Hello name, 65 00:03:14,193 --> 00:03:17,792 and then we want to have a space here. 66 00:03:17,793 --> 00:03:19,890 So this is a white space. 67 00:03:19,891 --> 00:03:23,570 So name, white space, surname. 68 00:03:23,571 --> 00:03:27,683 And it's as easy as that. Execute, 69 00:03:27,684 --> 00:03:30,666 [Author typing] 70 00:03:30,683 --> 00:03:32,202 and you get the output. 71 00:03:32,203 --> 00:03:34,710 Hello, Ardit, Sulce. 72 00:03:34,711 --> 00:03:36,888 So that's the message variable, the 73 00:03:36,889 --> 00:03:38,574 string of the message variable. 74 00:03:38,575 --> 00:03:40,936 So in this way, you can put 75 00:03:40,937 --> 00:03:43,140 as many variables as you like. 76 00:03:44,710 --> 00:03:46,680 For example, what's up? 77 00:03:47,610 --> 00:03:53,228 When could be a variable today. 78 00:03:53,229 --> 00:03:56,194 The string today is part of the when variable. 79 00:03:56,195 --> 00:03:59,628 The when variable will be replaced in here by the 80 00:03:59,629 --> 00:04:03,772 value that it has, it currently has, which is today. 81 00:04:03,773 --> 00:04:05,000 Execute that. 82 00:04:05,001 --> 00:04:07,866 [Author typing] 83 00:04:07,866 --> 00:04:10,236 And that's how it works. 84 00:04:10,237 --> 00:04:11,428 And that's what you need to 85 00:04:11,429 --> 00:04:13,930 know about string formatting in Python. 86 00:04:13,931 --> 00:04:17,600 [Author typing]