Let’s take a look at how the str.format() function works in Python. You’ll also find out how you can use this format in your own programs.
How Does the Python String format() Work?
The Python str.format() function lets you insert your results anywhere in a string.
It works by allocating space for your results within the string using curly braces. It then writes your output to that position using the format() method.
The strings outside the brace are what you call literal texts.
How to Use Python String format() Method
The str.format() function accepts a value and an optional string format specifier.
Here’s what the general syntax looks like:
Now let’s take a look at more practical examples of how to use this Python string method.
1. Insert a Value in a Specific Position
Here’s a basic example of how to insert a value into a string using the str.format() method:
The above code is the same as:
2. Insert Multiple Outputs Into a String
You can also insert multiple results into a string. Here’s how:
3. Use Escape Braces to Place Outputs in Dedicated Braces
If you need to place any of the results in a curly bracket, that’s easy. You only need to introduce two additional escaping braces.
For instance, say you want y in a dedicated curly brace:
4. Output Values From a List
You can select specific values from a list and insert them into a string:
To avoid repetition within the .format() parenthesis, you can set the format specifier to point to a single variable instead.
Here’s how to do that:
The above instance applies to other examples we’ve treated earlier, too. So, you can play around with them using this trick.
5. Insert Values From a Dictionary
Similarly, as you did in the previous section, you can use the str.format() method to insert a dictionary value into a string:
And if you want to use the trick from the previous section:
You can also write the above code as:
6. Insert the Output of a Function Into a String
If you want to show the output of a function in a string:
Using Python String Format Specifiers
The format specifier lets you choose how your format comes through. As mentioned earlier, it’s an optional parameter that comes with str.format().
Using this option, you can align your output, cut down long strings, group outputs, or round an integer to a specific number of significant figures, and even more.
You’ll often write the format specifiers inside the curly braces. But you can also state them explicitly inside the .format() parenthesis.
Let’s go ahead and see some of its use cases.
7. Align String Output
You can use the greater (>) sign to align a string output to the right:
You can also align your text to the center if you want:
Let’s format the above output further. For instance, you can include underscores to see the padding on either side of your string output:
But as earlier mentioned, you can state the format specifier explicitly as a parameter within str.format().
So, the previous code looks like this in that case:
Feel free to rewrite the other examples using the above option.
8. Format Outputs to a Specific Number of Significant Figures
You might also want to return a particular number of significant figures for a calculation using the .format() method.
The example code below, for instance, rounds the result of the mathematical operation to one significant decimal number:
9. Truncate Long Strings
Although truncating a text may seem impractical, you can’t tell where you might need it.
Here’s how to cut off part of your string output using the str.format() function:
10. Separate Group of Numbers Using a Criteria
You can separate a group of numbers using an underscore or a comma:
Moreover, you can specify the group of numbers you want to treat using its key:
Present Outputs Nicely With Python String format() Method
One of the ways you can make your program stand out is how you present results and queries. Undoubtedly, the Python string format method offers a cleaner way to output results. Unlike the previous modulo method of old Python versions, the new string format introduced in Python 3 is more readable and human-friendly.