Python format() Method Examples – Programming Code Examples
Python

Python format() Method Examples6 min read

String Format Python Example

To make sure a string will display as expected, we can format the result with the format() method.

The format() method allows you to format selected parts of a string.




Sometimes there are parts of a text that you do not control, maybe they come from a database, or user input?

 

Basic formatting with format()

Example: Add a placeholder where you want to display the count:

Output:

 

Example: In the following example, we format three strings.

Output:

 

Example:

Output:

 

String formatting by index numbers

Example: You can use index numbers (a number inside the curly brackets {0}) to be sure the values are placed in the correct placeholders:

Output:

 

String formatting by Named Indexes

It is also possible to make use of variables inside the curly brackets, as shown in the example below. The variables are defined inside format(). Therefore, when it executes, the value assigned to the variable is replaced inside the original string.

Example:

Output:

 

Example:

Output:

Numbers formatting with format()

Example: Simple number formatting

Output:

 

Example: It will give the output in decimal format when used inside the placeholder

Output:

 

Example :It will give the output in binary format when used inside the placeholder

 

Example : It will give the output in scientific format when used inside the placeholder, the exponent e in the output will be lowercase.

Output:

 

Example: It will give the output in scientific format when used inside the placeholder, the exponent E in the output will be uppercase

Output:

 

Example: This will output a fixed-point number format. By default, you will get the output of any number with six decimal places. In case you need up to 2 decimal places, use it as. 2f i.e.. a period (.) in front of 2f

Output:

 

Example: 

Output:

 

Example: This will output octal format

Output:

 

Example: This will output hex format in lowercase

Output:

 

Example: This will output hex format in uppercase.

Output:

 

Example: This will output number format.

 

Example: This will give the output in a percentage format. By default it will give 6 decimal places for the percentage output, in case you don’t want any decimal value you can use period with 0 i.e (:.0%).

Output:

 

Example: This will output an underscore as a thousand separator. It is available from python 3.6+.

Output:

 

Example: This will output comma as a thousands separator

Output:

 

Example: This example shows how to add space or padding before the given number. The number 5 indicates the space count you want before the number.

 

Example: The example shows how to get the output with a plus (+) sign before the number using {:+}.

 

Example: The example shows how to get the output with a plus (+/-) sign before equal to sign using {:=}.

 

Example: The example shows to use {:^} to center align the text. The number 10 is used to add 10 spaces to show the center-aligned when the value is replaced.

Output:

 

Example: The space of 10 is added using (:>10), and the value replaced is right-aligned.

Output:

 

Example : The space of 10 is added using (:<10), and the value replaces is left aligned.

 

Formatting class and dictionary members using format()

Example: Formatting class members using format()

Output:

 

Example : Formatting dictionary members using format()

Output:

 

Padding Variable Substitutions

Example: In below example will add space inside the Placeholder using the format(). To add space, you have to specify the number of spaces inside curly brackets after the colon(:). So the Placeholder will look like {:5}.

Output:

 

Example: You can also give the index inside the placeholder for example: {0:5} where 0 will refer to the first value inside format.

Output:

 

Example : Number formatting with padding for int and floats

Output:

 

 

 

 

 

Leave a Comment