Like in this lecture, I want to talk about the strings and in particular ways to print strings out
into the show and format those printouts.
Before we begin, I just want to show you an interesting module that I found recently, and that is
the string module, which is available with C Python, but not with micro python.
And what this does is to provide to a number of string constants that can be useful in some particular
cases.
For example, if you want to have the ability to quickly get a list of all ASCII located characters,
and you can do so by calling this constant.
Here to give you an example is that by importing the string module like this and then you can say the
string don't ASCII on this call uppercase and that will give you all uppercase characters.
Same thing with digits.
I'm just going to copy this time.
String digits will give you digits.
So I thought it's a useful module for some applications.
That interesting to show.
But let's go into the main part of what this lecture is about.
And that, again, as I said, it's printing now my sources for what I'm about to show you in case you
want to go deeper and do a bit of study on your own from the Python three documentation.
The first one is from the informal introduction to Python.
And I'm looking at the strings component here, strings section.
And I'm also looking at inputs and outputs, which is also part of the input output tutorial.
And eventually I'm going to show you a few things out of the format specification, mini language,
which is probably the most powerful way to format the strings in Python.
And Python is on its own and extremely capable language for anything that has to do with string manipulation
and string processing.
So let's begin with a little example.
Let's say that you have a bunch of variables.
So one of those is A that contains the screen.
Another one is B, which contains numerical integer one.
And it's at C, which contains, of course, the world actually looking at the space there just like
that.
Now, let's say that you want to print it out.
So the easiest way to print strings out is to use the print command.
Now the print command allows you to do things such as add variables like that.
You can add multiple variables as well.
So you just can add a space here and then add variable C.
And what this will do now is create one single print out where it's called the contents of the two variables,
A and C plus a string that I typed in manually in between, and those are separated by commas.
So that way you can write a program that can do various calculations to the results of those calculations
in variables.
And those drivers, of course, can be strings or numerical and then use a single print command when
you add all those variables together via commerce.
So that comma separated and then you have a single printout.
You can also had no so not confined to printing out variables of the same data type.
So I'm going to add another comma, this time in between double quotes and then print out B or the value
stored in both B, which is a numerical one and it looks like this.
So this is perhaps the easiest way to print out a string or a combination of strings that don't require
much formatting.
One more thing that I want to show you is that you can also add special characters.
So, for example, let's say that you want to have a line break between hello and world, which you
can do it.
You can add a new line special character which is shaped by the backslash, and that will give you a
line said right there, then break the rest of the string, print out into a separate line with others.
For example, you can add a backslash T and that will give you a tab.
You can see that the numerical one is pushed out towards the right.
One more thing that I want to show you before we move on to the next item in my list to talk about in
this lecture is that you can also use the backslash escape character to produce things such as a single
quote.
I could put it like that and.
Then, of course, they care, of course, the ticket should be after the escape character like this
and let's close it this way.
So now our print out appears like this and there is no one at peering enclosed inside single quotes.
But of course, you can use double quotes as well if you wish.
So as they are after the escape character, which is the backslash narratives like this.
So the print function on its own allows you to create strings that are composed of individual components,
either values stored in variables or explicitly included in the print statement.
And you've got all those components separated by commas.
You can also use the escape character to create things such as a new line or a tab or single or double
quotes.
Now, if you want to add formatting, then in this course will be using the format specification in
the language.
I'm going to give you some examples if you want to format your string, not just to print it.
And I recommend that you learn the format specification.
Middle language is going to give you some examples.
And as you'll see, it's very simple and we'll be using it throughout this course.
So the formal specification language looks like this.
You've got a string.
Just a regular string, and in that string, you've got a curly bracket open and close which create
placeholders and a few different kinds of placeholders, but the simplest one is this one is the anonymous
placeholder.
You have as many of those as you want, and then you need to use the format function on the string.
You can see that string and you've got a dot notation to bring up its format function.
And then inside the format function, you list the things that you want to use in order to replace the
placeholders that you have placed in the string.
So in this case, we've got this string to placeholders.
Three other items that will be used to replace as placeholders in the end result looks like this.
And apart from the anonymous placeholder, you also have the ability to enumerate them so you can give
them a number.
Or we can give them a name.
We can give them format's as well, as you can see right here.
We've got a placeholder which will be formatted as a digital renumber.
Let me show you a few examples that I think to remember here is that the print function and the format,
many language works exactly the same on Python as it does on micro python.
So everything I'm going to show you works in both types of python.
So I'm going to copy a little bit of text here.
So it saves me the typing.
So there's a little string.
Can print it out.
Using the print command and you get what you expected, Michael Python on the E.S.P thirty two Teletón
hands and that by including a placeholder.
So in this case I've got the placeholder with my open and close curly brackets and that becomes a variable.
I want to create one more variable.
Named a..
And then I'm going to do printing, so what I've got here is the string, which remember contains this
with the placeholder, inserted a single placeholder, and then I'm calling the format function and
I am passing another variable that is variable.
Being inside the form and function means that it will be used or will get its value to replace the single
placeholder.
So once that is evaluated, we get this completed string.
OK, so that is pretty cool.
It means that if we change.
Something in our expression like this, we've got an updated adjective, then the print command will
re-evaluate the expression and give us a new string.
Now, if you have a single location where you want to place some string that comes from a variable,
the expression is not complex.
So you don't need to worry about the anonymity of the placeholder.
But if you have more, then it's a good practice and a good idea to start naming your placeholders.
So I'm going to give you an example of how that can be done.
So I'm going to recreate or reset the my string variable.
But this time, instead of using an anonymous placeholder, we'll give it a name.
So we put the name inside the curly brackets like this so we can have as many of those as you want.
Each one will have its own name.
The adjectives just going to reset it to a single word.
Amazing.
And now to have a look at the print statement, remember that the place for the now has a name, so
inside format, I need to use the name of the placeholder in order to pass the variable that I want
to use at its place.
So enter.
And the result is exactly the same.
The only difference is that the placeholder now has a name.
And once you get the hang of this, then the complexity can increase.
So in this example, expanding on my string, I've got a target as well as a word that allows me to
have two variables inside this string.
So the adjective is still amazing, let's come up with an MCU you now here's a new variable MCU, which
you say is considered too.
And now the print statement, of course, needs to address the two placeholders, the two named placeholders,
a word and the target a word, and putting the adjective which we've already found a bit earlier.
And for the target, I'm going to pass MCU, which is defined right here.
And there you go, I could python on the East Coast.
It is amazing.
And we're going to play with numbers, and that's where formatting will come in and show you a few examples
there.
So let's start with a let's make that a 13 point number.
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
to do a calculation.
Let's make the equals eight times.
Make the times see.
All right, so if you say print the you will get this number.
You see how many decimals it has at four point eight with a large number of zeroes.
So I can create a string out of this calculation and out of these numbers.
So I'm going to say a calculation
is equal and now I'm going to use placeholders.
So I'm going to go for number one.
Times to.
Equals.
The result is one result.
So we've got a string.
The string is composed or contains three placeholders with their individual names.
You know, I can do a print, so let's print out a calculation.
It just hit the tab key and then Anthony completed the rest of my expression here, the name of the
variable adult format, a couple of letters out of the word format, and then the turkey and Thorney
will give me a choice of functions that match at that point.
So I'm going to format and open parentheses and only to put in the appropriate placeholder names, which
I've got no one for, no one.
I'm going to say that I want to pass the variable for detail and for number two possible before see.
And for result, I'm going to actually do the calculation.
I can either do the calculation here in line as this bad times see, or I can just say D since I have
defined that calculation up here.
All right.
And close parentheses and then close the second set of parentheses and enter and now we get this very
long string of text, but I would like to do next is to format these numbers.
So the first two numbers don't look that, but the result looks pretty bad.
I just want to go, say, up to two decimals maximum.
So to do that, need to go back into the a calculation definition and use the format expressions.
In this case, I want to use the zero point two expression of floating point number, which means that
what is printed out of this placeholder is a floating point number with two decimal points after the
dot.
Someone do the same thing for zero four zero, so there's going to be zero point two as.
And finally, for the results, definitely this one I wanted to be sure to point to yes.
For the floating point number of the result.
Now, let's reprint and you get a much better looking result here.
One point fifty three times three point twenty equals four point eight zero.
So this is an example of what you can do with the format function.
As I said, I'll be using it a lot in the remainder of this course.
And I encourage you to have a look at this documentation here for the format specification in any language,
because there's a lot more to it than what I've shown you.
What I've shown you is enough for the purposes of this course.
But if you want to dig into it and learn more details and documentation, here is where you need to
get.