In python, the string format() method is useful to format complex strings and numbers. The format strings will contain the curly braces { }
and the format()
method will use those curly braces { }
as placeholders to replace with the content of the parameters.
Following is the pictorial representation of string format()
method functionality in python.
If you observe the above diagram, the format string (s
) contains placeholders ({ }
), and the python format
method inserted the required text in the place of placeholders and returned the formatted string.
Following is the syntax of defining a string format method in python.
The python format() method will accept unlimited arguments and replace the curly brace { }
placeholders with the content of the respective arguments.
Following is the example of formatting the strings using the format()
method in python.
If you observe the above example, we created strings with a curly brace { }
placeholders and we are replacing the placeholders with required argument values using format()
method.
When you execute the above python program, you will get the result as shown below.
If you observe the above example, we created strings with placeholders without specifying any order, and we are not sure whether the arguments are placed in the correct order or not.
To make sure the arguments are placed in the correct order, you can define the positional or keyword arguments in the placeholders like as shown below.
If you observe the above example, we defined the placeholders with positional and keyword arguments to specify the order.
When you execute the above python program, you will get the result as shown below.
In python, you can format the placeholder's text by using different formatting types like <, >, ^, etc., based on your requirements.
The following table lists different type of format types you can use inside of placeholders to format the text.
Type | Description |
---|---|
< | It will left align the result to the remaining space. |
> | It will right-align the result to the remaining space. |
^ | It will center align the result to the remaining space. |
+ | It is useful to indicate whether the result is positive or negative |
- | It is useful to indicate the result is negative |
= | It will place the sign (+/-) to the leftmost position. |
_ | It will be used as a thousands separator. |
, | It will be used as a thousands separator |
b | It will convert number to binary format |
c | It will convert number to Unicode character |
d | It will convert number to decimal format |
e | It will convert number to exponential notation (lowercase e) |
E | It will convert number to exponential notation (uppercase E) |
f | It will convert number to fixed-point number |
F | It's same as 'f', but it displays inf as INF and nan as NAN |
g | It will show in a general format |
G | It's same as 'g', but it will use upper case E for exponential notation |
n | It will convert to number format, and it's same as 'd'. |
o | It will convert number to octal format |
x | It will convert number to hexadecimal format (lower case) |
X | It will convert number to hexadecimal format (upper case) |
% | It will convert the number into a percentage format. |
Following is the example of using formatting the strings using different format types in python.
If you observe the above example, we used different format types in placeholders { }
to format the given string values based on requirements.
When you execute the above python program, you will get the result as shown below.
This is how you can use the string format() method in python to format the strings based on your requirements.