1
00:00:00,690 --> 00:00:06,150
Like in this lecture, I want to talk about the strings and in particular ways to print strings out

2
00:00:06,150 --> 00:00:09,480
into the show and format those printouts.

3
00:00:10,230 --> 00:00:15,120
Before we begin, I just want to show you an interesting module that I found recently, and that is

4
00:00:15,120 --> 00:00:21,080
the string module, which is available with C Python, but not with micro python.

5
00:00:21,300 --> 00:00:27,960
And what this does is to provide to a number of string constants that can be useful in some particular

6
00:00:27,960 --> 00:00:28,400
cases.

7
00:00:28,410 --> 00:00:34,030
For example, if you want to have the ability to quickly get a list of all ASCII located characters,

8
00:00:34,050 --> 00:00:37,200
and you can do so by calling this constant.

9
00:00:37,200 --> 00:00:45,560
Here to give you an example is that by importing the string module like this and then you can say the

10
00:00:45,570 --> 00:00:56,130
string don't ASCII on this call uppercase and that will give you all uppercase characters.

11
00:00:56,910 --> 00:00:57,950
Same thing with digits.

12
00:00:57,960 --> 00:00:59,130
I'm just going to copy this time.

13
00:01:00,090 --> 00:01:02,930
String digits will give you digits.

14
00:01:03,450 --> 00:01:07,330
So I thought it's a useful module for some applications.

15
00:01:07,330 --> 00:01:08,400
That interesting to show.

16
00:01:08,910 --> 00:01:12,200
But let's go into the main part of what this lecture is about.

17
00:01:12,210 --> 00:01:20,100
And that, again, as I said, it's printing now my sources for what I'm about to show you in case you

18
00:01:20,100 --> 00:01:25,550
want to go deeper and do a bit of study on your own from the Python three documentation.

19
00:01:25,560 --> 00:01:28,830
The first one is from the informal introduction to Python.

20
00:01:28,830 --> 00:01:33,090
And I'm looking at the strings component here, strings section.

21
00:01:33,420 --> 00:01:39,630
And I'm also looking at inputs and outputs, which is also part of the input output tutorial.

22
00:01:40,080 --> 00:01:45,330
And eventually I'm going to show you a few things out of the format specification, mini language,

23
00:01:45,780 --> 00:01:51,870
which is probably the most powerful way to format the strings in Python.

24
00:01:52,350 --> 00:02:00,780
And Python is on its own and extremely capable language for anything that has to do with string manipulation

25
00:02:00,780 --> 00:02:02,440
and string processing.

26
00:02:03,240 --> 00:02:06,480
So let's begin with a little example.

27
00:02:06,480 --> 00:02:09,300
Let's say that you have a bunch of variables.

28
00:02:09,510 --> 00:02:15,060
So one of those is A that contains the screen.

29
00:02:16,410 --> 00:02:21,180
Another one is B, which contains numerical integer one.

30
00:02:21,450 --> 00:02:30,210
And it's at C, which contains, of course, the world actually looking at the space there just like

31
00:02:30,210 --> 00:02:30,450
that.

32
00:02:31,170 --> 00:02:33,220
Now, let's say that you want to print it out.

33
00:02:33,540 --> 00:02:38,980
So the easiest way to print strings out is to use the print command.

34
00:02:39,000 --> 00:02:44,100
Now the print command allows you to do things such as add variables like that.

35
00:02:44,580 --> 00:02:46,640
You can add multiple variables as well.

36
00:02:46,650 --> 00:02:51,920
So you just can add a space here and then add variable C.

37
00:02:52,530 --> 00:02:59,220
And what this will do now is create one single print out where it's called the contents of the two variables,

38
00:02:59,220 --> 00:03:09,330
A and C plus a string that I typed in manually in between, and those are separated by commas.

39
00:03:10,410 --> 00:03:17,580
So that way you can write a program that can do various calculations to the results of those calculations

40
00:03:17,580 --> 00:03:18,840
in variables.

41
00:03:18,840 --> 00:03:25,950
And those drivers, of course, can be strings or numerical and then use a single print command when

42
00:03:25,950 --> 00:03:30,690
you add all those variables together via commerce.

43
00:03:30,990 --> 00:03:34,400
So that comma separated and then you have a single printout.

44
00:03:34,950 --> 00:03:42,210
You can also had no so not confined to printing out variables of the same data type.

45
00:03:42,660 --> 00:03:50,400
So I'm going to add another comma, this time in between double quotes and then print out B or the value

46
00:03:50,400 --> 00:03:54,850
stored in both B, which is a numerical one and it looks like this.

47
00:03:55,950 --> 00:04:04,470
So this is perhaps the easiest way to print out a string or a combination of strings that don't require

48
00:04:04,680 --> 00:04:06,450
much formatting.

49
00:04:06,990 --> 00:04:11,910
One more thing that I want to show you is that you can also add special characters.

50
00:04:11,910 --> 00:04:18,720
So, for example, let's say that you want to have a line break between hello and world, which you

51
00:04:18,720 --> 00:04:19,670
can do it.

52
00:04:19,680 --> 00:04:29,910
You can add a new line special character which is shaped by the backslash, and that will give you a

53
00:04:30,120 --> 00:04:38,420
line said right there, then break the rest of the string, print out into a separate line with others.

54
00:04:38,430 --> 00:04:45,060
For example, you can add a backslash T and that will give you a tab.

55
00:04:45,600 --> 00:04:50,640
You can see that the numerical one is pushed out towards the right.

56
00:04:51,900 --> 00:04:57,720
One more thing that I want to show you before we move on to the next item in my list to talk about in

57
00:04:57,720 --> 00:05:08,430
this lecture is that you can also use the backslash escape character to produce things such as a single

58
00:05:08,430 --> 00:05:08,970
quote.

59
00:05:09,750 --> 00:05:12,930
I could put it like that and.

60
00:05:14,620 --> 00:05:20,560
Then, of course, they care, of course, the ticket should be after the escape character like this

61
00:05:20,560 --> 00:05:24,970
and let's close it this way.

62
00:05:25,570 --> 00:05:35,290
So now our print out appears like this and there is no one at peering enclosed inside single quotes.

63
00:05:35,290 --> 00:05:38,190
But of course, you can use double quotes as well if you wish.

64
00:05:38,230 --> 00:05:46,620
So as they are after the escape character, which is the backslash narratives like this.

65
00:05:47,440 --> 00:05:57,040
So the print function on its own allows you to create strings that are composed of individual components,

66
00:05:57,040 --> 00:06:03,700
either values stored in variables or explicitly included in the print statement.

67
00:06:03,820 --> 00:06:06,350
And you've got all those components separated by commas.

68
00:06:07,150 --> 00:06:15,490
You can also use the escape character to create things such as a new line or a tab or single or double

69
00:06:15,640 --> 00:06:16,180
quotes.

70
00:06:16,930 --> 00:06:26,560
Now, if you want to add formatting, then in this course will be using the format specification in

71
00:06:26,560 --> 00:06:27,280
the language.

72
00:06:27,820 --> 00:06:35,980
I'm going to give you some examples if you want to format your string, not just to print it.

73
00:06:35,980 --> 00:06:38,380
And I recommend that you learn the format specification.

74
00:06:38,380 --> 00:06:40,650
Middle language is going to give you some examples.

75
00:06:40,660 --> 00:06:45,020
And as you'll see, it's very simple and we'll be using it throughout this course.

76
00:06:45,520 --> 00:06:50,050
So the formal specification language looks like this.

77
00:06:50,560 --> 00:06:52,090
You've got a string.

78
00:06:53,470 --> 00:07:00,820
Just a regular string, and in that string, you've got a curly bracket open and close which create

79
00:07:00,820 --> 00:07:06,850
placeholders and a few different kinds of placeholders, but the simplest one is this one is the anonymous

80
00:07:06,850 --> 00:07:07,690
placeholder.

81
00:07:08,450 --> 00:07:16,810
You have as many of those as you want, and then you need to use the format function on the string.

82
00:07:16,990 --> 00:07:23,610
You can see that string and you've got a dot notation to bring up its format function.

83
00:07:23,980 --> 00:07:31,870
And then inside the format function, you list the things that you want to use in order to replace the

84
00:07:31,870 --> 00:07:35,740
placeholders that you have placed in the string.

85
00:07:36,430 --> 00:07:41,170
So in this case, we've got this string to placeholders.

86
00:07:42,870 --> 00:07:47,930
Three other items that will be used to replace as placeholders in the end result looks like this.

87
00:07:48,870 --> 00:07:55,380
And apart from the anonymous placeholder, you also have the ability to enumerate them so you can give

88
00:07:55,380 --> 00:07:56,280
them a number.

89
00:07:57,610 --> 00:07:58,810
Or we can give them a name.

90
00:08:00,600 --> 00:08:04,120
We can give them format's as well, as you can see right here.

91
00:08:04,140 --> 00:08:09,510
We've got a placeholder which will be formatted as a digital renumber.

92
00:08:11,260 --> 00:08:18,670
Let me show you a few examples that I think to remember here is that the print function and the format,

93
00:08:18,670 --> 00:08:24,500
many language works exactly the same on Python as it does on micro python.

94
00:08:24,520 --> 00:08:27,690
So everything I'm going to show you works in both types of python.

95
00:08:28,780 --> 00:08:31,600
So I'm going to copy a little bit of text here.

96
00:08:31,750 --> 00:08:33,530
So it saves me the typing.

97
00:08:34,360 --> 00:08:36,790
So there's a little string.

98
00:08:38,100 --> 00:08:39,050
Can print it out.

99
00:08:41,130 --> 00:08:48,360
Using the print command and you get what you expected, Michael Python on the E.S.P thirty two Teletón

100
00:08:48,390 --> 00:08:52,040
hands and that by including a placeholder.

101
00:08:52,050 --> 00:08:59,790
So in this case I've got the placeholder with my open and close curly brackets and that becomes a variable.

102
00:09:00,510 --> 00:09:02,580
I want to create one more variable.

103
00:09:05,090 --> 00:09:06,260
Named a..

104
00:09:08,250 --> 00:09:17,160
And then I'm going to do printing, so what I've got here is the string, which remember contains this

105
00:09:17,160 --> 00:09:24,270
with the placeholder, inserted a single placeholder, and then I'm calling the format function and

106
00:09:24,270 --> 00:09:27,600
I am passing another variable that is variable.

107
00:09:27,600 --> 00:09:33,960
Being inside the form and function means that it will be used or will get its value to replace the single

108
00:09:33,960 --> 00:09:34,750
placeholder.

109
00:09:35,610 --> 00:09:40,650
So once that is evaluated, we get this completed string.

110
00:09:41,460 --> 00:09:43,920
OK, so that is pretty cool.

111
00:09:44,460 --> 00:09:46,230
It means that if we change.

112
00:09:47,990 --> 00:09:59,330
Something in our expression like this, we've got an updated adjective, then the print command will

113
00:10:00,380 --> 00:10:03,740
re-evaluate the expression and give us a new string.

114
00:10:04,370 --> 00:10:13,070
Now, if you have a single location where you want to place some string that comes from a variable,

115
00:10:13,280 --> 00:10:14,890
the expression is not complex.

116
00:10:14,900 --> 00:10:20,540
So you don't need to worry about the anonymity of the placeholder.

117
00:10:20,990 --> 00:10:28,070
But if you have more, then it's a good practice and a good idea to start naming your placeholders.

118
00:10:28,610 --> 00:10:31,690
So I'm going to give you an example of how that can be done.

119
00:10:31,700 --> 00:10:36,860
So I'm going to recreate or reset the my string variable.

120
00:10:37,970 --> 00:10:42,650
But this time, instead of using an anonymous placeholder, we'll give it a name.

121
00:10:42,650 --> 00:10:49,200
So we put the name inside the curly brackets like this so we can have as many of those as you want.

122
00:10:49,220 --> 00:10:51,110
Each one will have its own name.

123
00:10:51,800 --> 00:10:57,260
The adjectives just going to reset it to a single word.

124
00:10:57,710 --> 00:10:58,430
Amazing.

125
00:10:59,590 --> 00:11:06,340
And now to have a look at the print statement, remember that the place for the now has a name, so

126
00:11:06,700 --> 00:11:16,870
inside format, I need to use the name of the placeholder in order to pass the variable that I want

127
00:11:16,870 --> 00:11:18,460
to use at its place.

128
00:11:19,360 --> 00:11:20,970
So enter.

129
00:11:21,010 --> 00:11:23,350
And the result is exactly the same.

130
00:11:23,980 --> 00:11:26,860
The only difference is that the placeholder now has a name.

131
00:11:29,000 --> 00:11:34,400
And once you get the hang of this, then the complexity can increase.

132
00:11:36,220 --> 00:11:46,270
So in this example, expanding on my string, I've got a target as well as a word that allows me to

133
00:11:46,480 --> 00:11:48,790
have two variables inside this string.

134
00:11:50,260 --> 00:11:56,610
So the adjective is still amazing, let's come up with an MCU you now here's a new variable MCU, which

135
00:11:56,610 --> 00:11:57,730
you say is considered too.

136
00:11:58,150 --> 00:12:08,740
And now the print statement, of course, needs to address the two placeholders, the two named placeholders,

137
00:12:08,770 --> 00:12:13,770
a word and the target a word, and putting the adjective which we've already found a bit earlier.

138
00:12:14,230 --> 00:12:18,930
And for the target, I'm going to pass MCU, which is defined right here.

139
00:12:20,660 --> 00:12:23,710
And there you go, I could python on the East Coast.

140
00:12:23,780 --> 00:12:24,980
It is amazing.

141
00:12:25,930 --> 00:12:32,380
And we're going to play with numbers, and that's where formatting will come in and show you a few examples

142
00:12:32,380 --> 00:12:32,650
there.

143
00:12:32,680 --> 00:12:37,870
So let's start with a let's make that a 13 point number.

144
00:12:37,870 --> 00:12:46,180
One point to be is going to be one point five and see, it's going to be three point two and it's off

145
00:12:46,180 --> 00:12:47,370
to do a calculation.

146
00:12:47,830 --> 00:12:53,410
Let's make the equals eight times.

147
00:12:55,790 --> 00:12:58,970
Make the times see.

148
00:13:00,190 --> 00:13:05,640
All right, so if you say print the you will get this number.

149
00:13:05,930 --> 00:13:12,040
You see how many decimals it has at four point eight with a large number of zeroes.

150
00:13:12,340 --> 00:13:18,040
So I can create a string out of this calculation and out of these numbers.

151
00:13:18,410 --> 00:13:21,730
So I'm going to say a calculation

152
00:13:24,130 --> 00:13:28,990
is equal and now I'm going to use placeholders.

153
00:13:29,030 --> 00:13:31,090
So I'm going to go for number one.

154
00:13:33,470 --> 00:13:35,900
Times to.

155
00:13:38,920 --> 00:13:39,760
Equals.

156
00:13:42,470 --> 00:13:45,570
The result is one result.

157
00:13:46,790 --> 00:13:48,050
So we've got a string.

158
00:13:49,390 --> 00:13:54,430
The string is composed or contains three placeholders with their individual names.

159
00:13:57,220 --> 00:14:04,480
You know, I can do a print, so let's print out a calculation.

160
00:14:06,340 --> 00:14:13,210
It just hit the tab key and then Anthony completed the rest of my expression here, the name of the

161
00:14:13,210 --> 00:14:23,440
variable adult format, a couple of letters out of the word format, and then the turkey and Thorney

162
00:14:23,440 --> 00:14:28,510
will give me a choice of functions that match at that point.

163
00:14:28,960 --> 00:14:36,310
So I'm going to format and open parentheses and only to put in the appropriate placeholder names, which

164
00:14:36,310 --> 00:14:37,960
I've got no one for, no one.

165
00:14:37,970 --> 00:14:48,490
I'm going to say that I want to pass the variable for detail and for number two possible before see.

166
00:14:50,290 --> 00:14:58,840
And for result, I'm going to actually do the calculation.

167
00:14:59,230 --> 00:15:08,680
I can either do the calculation here in line as this bad times see, or I can just say D since I have

168
00:15:08,680 --> 00:15:10,550
defined that calculation up here.

169
00:15:12,130 --> 00:15:12,610
All right.

170
00:15:14,540 --> 00:15:22,640
And close parentheses and then close the second set of parentheses and enter and now we get this very

171
00:15:22,640 --> 00:15:29,840
long string of text, but I would like to do next is to format these numbers.

172
00:15:29,870 --> 00:15:34,260
So the first two numbers don't look that, but the result looks pretty bad.

173
00:15:34,340 --> 00:15:37,940
I just want to go, say, up to two decimals maximum.

174
00:15:38,690 --> 00:15:48,340
So to do that, need to go back into the a calculation definition and use the format expressions.

175
00:15:48,680 --> 00:15:56,180
In this case, I want to use the zero point two expression of floating point number, which means that

176
00:15:56,690 --> 00:16:06,440
what is printed out of this placeholder is a floating point number with two decimal points after the

177
00:16:06,440 --> 00:16:06,740
dot.

178
00:16:07,810 --> 00:16:13,290
Someone do the same thing for zero four zero, so there's going to be zero point two as.

179
00:16:15,050 --> 00:16:22,840
And finally, for the results, definitely this one I wanted to be sure to point to yes.

180
00:16:24,170 --> 00:16:27,110
For the floating point number of the result.

181
00:16:28,910 --> 00:16:34,470
Now, let's reprint and you get a much better looking result here.

182
00:16:34,520 --> 00:16:40,500
One point fifty three times three point twenty equals four point eight zero.

183
00:16:41,960 --> 00:16:45,680
So this is an example of what you can do with the format function.

184
00:16:45,680 --> 00:16:50,840
As I said, I'll be using it a lot in the remainder of this course.

185
00:16:50,840 --> 00:16:57,440
And I encourage you to have a look at this documentation here for the format specification in any language,

186
00:16:57,440 --> 00:17:00,800
because there's a lot more to it than what I've shown you.

187
00:17:00,980 --> 00:17:04,850
What I've shown you is enough for the purposes of this course.

188
00:17:04,850 --> 00:17:11,060
But if you want to dig into it and learn more details and documentation, here is where you need to

189
00:17:11,060 --> 00:17:11,240
get.
