The next topic we will be discussing about is formatting strings.
There might be a requirement to print the output in a specified format.
So for that you will want to control the way in which the output of a program is written and in Python.
There are several ways to present the output of a program.
We will discuss two ways in which the output can be formatted.
One is using the modulo operator and the other is string format method let us take an example to understand
the modulo operator formatting.
So consider this string as one which is pointing to the text.
My name is spaces in these spaces.
I want to insert the name Eric which is a string and in order for them to write an interpreter to know
that I want to insert a string here I use the modulo operator and this modulo operator is followed by
a data type that need to be formatted or converted for strings.
I'm going to use the format specify it is and now I want to insert the value Eric.
So this part of the string is called a format string and these are the values we knew that would be
used for inserting into the format string next the format string and the values are separated by and
not the modulo operator and the modulo operator helps to substitute this with the values here and in
shard.
This is the format of the modulo operator.
It is the Ford month during modulo operator and then you specify divide moves to the right.
If you have multiple values that you need to substitute you can include them in parenthesis value 1
while you two and so on.
So in my Jupiter book I have given the string as one as mine name is modulo s and this is the value
that I want to substitute modulo s with and I am separating the four months string and the value added
with the modulo operator
and no let print as one.
So as you can see this modulo operator is has been substituted with the value to the right.
Now let's so consider that the string S1 is now mine name is spaces I am spaces yours or and your.
In this example in this spaces I want to insert a string and your word I want to insert a number so
using the modulo operator since I want to insert a string in these spaces I use a modulo operator and
followed by S which means I want to insert a string and then in the next show in the next position I
want to insert a number.
So here at I would give modulo operator and then D to specify I want to insert a number here and as
we have discussed before if there are multiple values that we need to insert we can include them in
parentheses separating the values with commas the first value would be Eric
the second value is 7 and we need to separate the format string and the format string here and the values
with modulo operator.
And this helps substitute do values so let's bring this out in Jupiter notebook.
So here is of a string is one which has to format specify as a string and then in digit.
And accordingly we have provided to use.
Let us now execute this line of code.
Output the variable is 1.
So this is how we can substitute multiple values using the modulo operator.
Now let's go look into another example.
Using the modulo operator we can also find my floating point numbers.
Say for example in this string is to is binding to detect the cartoon is spaces close and I want to
insert devalue forty five point six seven eight which is a floating point number.
So like we have done earlier we need to specify a modulo operator followed by the data a floating point
numbers it is F and then the fourth man string and these values have to be separated with a modulo operator.
So let's check this example in the Jupiter notebook.
So we have a string as to let us now see how the modulo operator works on floating by in numbers.
Let's output this variable is 2.
So till now we have seen how modulo operator format strings and then know integers and floating point
numbers.
We will now move to the next formatting method that is the string format method so for string formatting.
Let's take this string as one which contained this text My name is spaces.
So in these spaces I want to insert the name Eric.
So let's see how we can use these string format method to format strings similar to the modulo operator
in the modulo operator.
We have put in percentage is a percentage to whatever data type that needs to be inserted here.
And in case of the string format method we use curly braces nada to specify that the value needs to
be inserted in this position and then to the right hand side we specify the values that needs to being
sorted in normal case it's the name Eric.
So we have discussed that this is known as the Ford months during and these are the value.
While you are on values and these two are separated by dot format which is the method in case of the
modulo operator we have used the symbol in modern law but the format method we use dot format
so let's see how the 5 month string method operates on this particular string here.
So as as but our discussion I have the Ford months but and I used it curly braces to specify to insert
the values in the in the position of the economy braces and here is the ground loop that is used for
substitution so let us execute this name and then display the variable is one so here is a verb formatted
string let's discuss and not that example yet another example maybe want to insert a string and then
on numbered can define my string so he wrote in the first position I want to insert this string Eric
and then in the second position here I want to insert the number 7.
So using the format Metro began fired my these two and instead of specifying the values in the parentheses
here what we can do is we can stored the values in variables and then displays these values with these
variables A and B.
So here I have in the format method I have replace devalues edit and then the number seven with the
variables A and B and the same thing can be done with the modulo operator has been that we've discussed
before.
Let's see how this string can be formatted using the format method in Jupiter notebook.
So as we have discussed I have stored the name edit in the variable E and then B is equal to 7 and this
is my format string so instead of giving the values in the format method I have entered the variable.
Let's see how the format method works
and then I'll put debatable as 1.
So this is one example of how the one man method.
For months the strings and apart from this we can reference the variables on the right by offerings
in that index.
This can be done by placing the index number of the parameter in the curly braces.
So if you put Ziegel in the curly braces here would this access as the first parameter e in debate emphasis.
And if I put one in Nikolay braces it references the second parameter which is B.
So let us now execute the these lines of code.
So this is how were the variables that can be referred using that index numbers in the index number
begins from 0.
Next we will see how defined mounting method for months plotting find numbers
so here we have the string.
A store contains the text the carton is bases close.
So if we want to use the string format method as but a discussion we use the curly braces and then we
need to separate default monitoring and the values with the key word of the method.
Di Wildman and then let's bring this out in the statement you put the non so weird indeed you did not
book.
I have tied the text for the string and store.
Now let's plot my.
The string using defined mad meter execute this line of core here.
The curly braces which needs to be replaced with the floating find no human so let out what this red
have been is do.
You can also give the index name but here so let's give Zito that is the only value present in the in
the bed and persists.
Here for example we want to display the first two numbers after the decimal.
So in order to specify the data type with two decimal places after the index number we need to give
up gold in this gold and separate the index number from the data type and then because I want to print
only two decimal places I would give dot 2 and then the data type and index only braces and then let
execute this line then display the variable is to.
So this is how we can format string using the string format method.