1 00:00:00,270 --> 00:00:05,350 The next topic we will be discussing about is formatting strings. 2 00:00:05,400 --> 00:00:11,910 There might be a requirement to print the output in a specified format. 3 00:00:11,910 --> 00:00:19,900 So for that you will want to control the way in which the output of a program is written and in Python. 4 00:00:19,920 --> 00:00:23,980 There are several ways to present the output of a program. 5 00:00:24,030 --> 00:00:27,840 We will discuss two ways in which the output can be formatted. 6 00:00:27,840 --> 00:00:37,850 One is using the modulo operator and the other is string format method let us take an example to understand 7 00:00:38,210 --> 00:00:40,670 the modulo operator formatting. 8 00:00:41,510 --> 00:00:46,560 So consider this string as one which is pointing to the text. 9 00:00:46,580 --> 00:00:50,620 My name is spaces in these spaces. 10 00:00:50,630 --> 00:00:59,420 I want to insert the name Eric which is a string and in order for them to write an interpreter to know 11 00:00:59,720 --> 00:01:10,210 that I want to insert a string here I use the modulo operator and this modulo operator is followed by 12 00:01:10,270 --> 00:01:17,230 a data type that need to be formatted or converted for strings. 13 00:01:17,230 --> 00:01:26,130 I'm going to use the format specify it is and now I want to insert the value Eric. 14 00:01:26,200 --> 00:01:38,140 So this part of the string is called a format string and these are the values we knew that would be 15 00:01:38,530 --> 00:01:51,040 used for inserting into the format string next the format string and the values are separated by and 16 00:01:51,040 --> 00:02:04,000 not the modulo operator and the modulo operator helps to substitute this with the values here and in 17 00:02:04,000 --> 00:02:04,770 shard. 18 00:02:04,840 --> 00:02:09,200 This is the format of the modulo operator. 19 00:02:09,370 --> 00:02:18,070 It is the Ford month during modulo operator and then you specify divide moves to the right. 20 00:02:18,970 --> 00:02:27,190 If you have multiple values that you need to substitute you can include them in parenthesis value 1 21 00:02:28,660 --> 00:02:31,570 while you two and so on. 22 00:02:31,610 --> 00:02:44,240 So in my Jupiter book I have given the string as one as mine name is modulo s and this is the value 23 00:02:44,240 --> 00:02:55,150 that I want to substitute modulo s with and I am separating the four months string and the value added 24 00:02:55,760 --> 00:02:57,280 with the modulo operator 25 00:03:00,300 --> 00:03:03,140 and no let print as one. 26 00:03:03,150 --> 00:03:14,740 So as you can see this modulo operator is has been substituted with the value to the right. 27 00:03:14,940 --> 00:03:27,170 Now let's so consider that the string S1 is now mine name is spaces I am spaces yours or and your. 28 00:03:27,260 --> 00:03:38,580 In this example in this spaces I want to insert a string and your word I want to insert a number so 29 00:03:38,640 --> 00:03:49,280 using the modulo operator since I want to insert a string in these spaces I use a modulo operator and 30 00:03:49,490 --> 00:04:00,290 followed by S which means I want to insert a string and then in the next show in the next position I 31 00:04:00,290 --> 00:04:02,900 want to insert a number. 32 00:04:02,900 --> 00:04:15,290 So here at I would give modulo operator and then D to specify I want to insert a number here and as 33 00:04:15,290 --> 00:04:21,590 we have discussed before if there are multiple values that we need to insert we can include them in 34 00:04:21,590 --> 00:04:27,050 parentheses separating the values with commas the first value would be Eric 35 00:04:30,490 --> 00:04:41,860 the second value is 7 and we need to separate the format string and the format string here and the values 36 00:04:42,250 --> 00:04:44,230 with modulo operator. 37 00:04:44,590 --> 00:04:53,550 And this helps substitute do values so let's bring this out in Jupiter notebook. 38 00:04:53,590 --> 00:05:02,430 So here is of a string is one which has to format specify as a string and then in digit. 39 00:05:02,560 --> 00:05:06,450 And accordingly we have provided to use. 40 00:05:06,450 --> 00:05:10,300 Let us now execute this line of code. 41 00:05:11,280 --> 00:05:15,260 Output the variable is 1. 42 00:05:15,310 --> 00:05:22,310 So this is how we can substitute multiple values using the modulo operator. 43 00:05:22,330 --> 00:05:24,930 Now let's go look into another example. 44 00:05:26,040 --> 00:05:31,700 Using the modulo operator we can also find my floating point numbers. 45 00:05:31,700 --> 00:05:43,290 Say for example in this string is to is binding to detect the cartoon is spaces close and I want to 46 00:05:43,330 --> 00:05:50,500 insert devalue forty five point six seven eight which is a floating point number. 47 00:05:50,870 --> 00:05:59,930 So like we have done earlier we need to specify a modulo operator followed by the data a floating point 48 00:05:59,930 --> 00:06:10,280 numbers it is F and then the fourth man string and these values have to be separated with a modulo operator. 49 00:06:11,000 --> 00:06:15,000 So let's check this example in the Jupiter notebook. 50 00:06:15,350 --> 00:06:23,920 So we have a string as to let us now see how the modulo operator works on floating by in numbers. 51 00:06:24,190 --> 00:06:26,890 Let's output this variable is 2. 52 00:06:27,120 --> 00:06:37,670 So till now we have seen how modulo operator format strings and then know integers and floating point 53 00:06:37,670 --> 00:06:38,520 numbers. 54 00:06:38,570 --> 00:06:47,420 We will now move to the next formatting method that is the string format method so for string formatting. 55 00:06:47,440 --> 00:06:54,920 Let's take this string as one which contained this text My name is spaces. 56 00:06:55,380 --> 00:06:59,320 So in these spaces I want to insert the name Eric. 57 00:06:59,400 --> 00:07:09,360 So let's see how we can use these string format method to format strings similar to the modulo operator 58 00:07:10,230 --> 00:07:11,880 in the modulo operator. 59 00:07:11,940 --> 00:07:20,310 We have put in percentage is a percentage to whatever data type that needs to be inserted here. 60 00:07:20,430 --> 00:07:30,280 And in case of the string format method we use curly braces nada to specify that the value needs to 61 00:07:30,280 --> 00:07:40,170 be inserted in this position and then to the right hand side we specify the values that needs to being 62 00:07:40,170 --> 00:07:44,260 sorted in normal case it's the name Eric. 63 00:07:44,460 --> 00:07:53,720 So we have discussed that this is known as the Ford months during and these are the value. 64 00:07:53,820 --> 00:08:05,510 While you are on values and these two are separated by dot format which is the method in case of the 65 00:08:05,750 --> 00:08:17,510 modulo operator we have used the symbol in modern law but the format method we use dot format 66 00:08:20,940 --> 00:08:27,960 so let's see how the 5 month string method operates on this particular string here. 67 00:08:28,050 --> 00:08:39,630 So as as but our discussion I have the Ford months but and I used it curly braces to specify to insert 68 00:08:39,660 --> 00:08:46,200 the values in the in the position of the economy braces and here is the ground loop that is used for 69 00:08:46,200 --> 00:09:00,330 substitution so let us execute this name and then display the variable is one so here is a verb formatted 70 00:09:00,540 --> 00:09:13,740 string let's discuss and not that example yet another example maybe want to insert a string and then 71 00:09:14,080 --> 00:09:23,690 on numbered can define my string so he wrote in the first position I want to insert this string Eric 72 00:09:24,410 --> 00:09:29,850 and then in the second position here I want to insert the number 7. 73 00:09:30,050 --> 00:09:44,340 So using the format Metro began fired my these two and instead of specifying the values in the parentheses 74 00:09:44,340 --> 00:09:57,050 here what we can do is we can stored the values in variables and then displays these values with these 75 00:09:57,050 --> 00:09:59,200 variables A and B. 76 00:10:01,190 --> 00:10:09,020 So here I have in the format method I have replace devalues edit and then the number seven with the 77 00:10:09,020 --> 00:10:17,000 variables A and B and the same thing can be done with the modulo operator has been that we've discussed 78 00:10:17,090 --> 00:10:18,250 before. 79 00:10:18,290 --> 00:10:24,840 Let's see how this string can be formatted using the format method in Jupiter notebook. 80 00:10:24,860 --> 00:10:37,370 So as we have discussed I have stored the name edit in the variable E and then B is equal to 7 and this 81 00:10:37,370 --> 00:10:49,410 is my format string so instead of giving the values in the format method I have entered the variable. 82 00:10:49,820 --> 00:10:53,420 Let's see how the format method works 83 00:10:56,730 --> 00:11:00,330 and then I'll put debatable as 1. 84 00:11:00,450 --> 00:11:04,710 So this is one example of how the one man method. 85 00:11:05,820 --> 00:11:17,850 For months the strings and apart from this we can reference the variables on the right by offerings 86 00:11:17,850 --> 00:11:20,140 in that index. 87 00:11:20,220 --> 00:11:27,350 This can be done by placing the index number of the parameter in the curly braces. 88 00:11:27,570 --> 00:11:38,070 So if you put Ziegel in the curly braces here would this access as the first parameter e in debate emphasis. 89 00:11:38,070 --> 00:11:47,580 And if I put one in Nikolay braces it references the second parameter which is B. 90 00:11:47,720 --> 00:11:54,910 So let us now execute the these lines of code. 91 00:11:54,970 --> 00:12:04,510 So this is how were the variables that can be referred using that index numbers in the index number 92 00:12:05,260 --> 00:12:09,710 begins from 0. 93 00:12:09,720 --> 00:12:15,410 Next we will see how defined mounting method for months plotting find numbers 94 00:12:18,040 --> 00:12:20,330 so here we have the string. 95 00:12:20,350 --> 00:12:25,590 A store contains the text the carton is bases close. 96 00:12:25,780 --> 00:12:39,340 So if we want to use the string format method as but a discussion we use the curly braces and then we 97 00:12:39,340 --> 00:12:47,070 need to separate default monitoring and the values with the key word of the method. 98 00:12:47,170 --> 00:12:59,330 Di Wildman and then let's bring this out in the statement you put the non so weird indeed you did not 99 00:12:59,350 --> 00:12:59,870 book. 100 00:13:00,220 --> 00:13:03,800 I have tied the text for the string and store. 101 00:13:03,940 --> 00:13:05,060 Now let's plot my. 102 00:13:05,080 --> 00:13:12,240 The string using defined mad meter execute this line of core here. 103 00:13:12,260 --> 00:13:21,670 The curly braces which needs to be replaced with the floating find no human so let out what this red 104 00:13:21,670 --> 00:13:23,150 have been is do. 105 00:13:23,160 --> 00:13:33,240 You can also give the index name but here so let's give Zito that is the only value present in the in 106 00:13:33,250 --> 00:13:35,060 the bed and persists. 107 00:13:35,120 --> 00:13:40,010 Here for example we want to display the first two numbers after the decimal. 108 00:13:40,410 --> 00:13:49,320 So in order to specify the data type with two decimal places after the index number we need to give 109 00:13:49,760 --> 00:13:58,530 up gold in this gold and separate the index number from the data type and then because I want to print 110 00:13:58,740 --> 00:14:09,500 only two decimal places I would give dot 2 and then the data type and index only braces and then let 111 00:14:09,570 --> 00:14:16,000 execute this line then display the variable is to. 112 00:14:16,080 --> 00:14:23,640 So this is how we can format string using the string format method.