format( ) Function is used to as a placeholder for the values to be inserted.
x = 3 y = 4 z = x + y result = "The added value of {} and {} is {}" print(result.format(x, y, z))
So, what we have done is, taken the numbers '3' and '4' in two variables, 'x' and 'y'.
And stored the added value in a variable named 'z'.
Now, comes the main part, where we have declared a variable named 'result'. And assigned the String 'The added value of { } and { } is { }' to it.
The braces '{ }' in the above text are placeholders, that will be displaying some values as mentioned in the 'format( )' method.
And in the next line we see the 'format( )' method with the print statement.
So, the String variable 'result' calls the 'format( )' method. And what the 'format( )' method does is, based on the order it fills the braces '{ }' with values.
Also you can specify the order, based on what the values will be placed.
x = 3 y = 4 z = x + y result = "The added value of {1} and {0} is {2}" print(result.format(x, y, z))
Now, if you see the above output, we have changed the order in the format method and we got the new output.
Also, we can change the order using keyword.
x = 3 y = 4 z = x + y result = "The added value of {p} and {q} is {r}" print(result.format(p=x, q=y, r=z))
Now, what we have done is, given temporary names for variables 'x' - p, 'y' - q and 'z' - r in the 'format( )' method.
And they got formatted accordingly.