python string formatting
format() method allows you format string in any way you want.
Syntax: template.format(p1, p1, .... , k1=v1, k2=v2)
template is a string containing format codes, format() method uses it’s argument to substitute value for each format codes. For e.g
{0} and {1} are format codes. The format code {0} is replaced by the first argument of format() i.e 12 , while {1} is replaced by the second argument of format() i.e 31 .
Expected Output:
This technique is okay for simple formatting but what if you want to specify precision in floating point number ? For such thing you need to learn more about format codes. Here is the full syntax of format codes.
Syntax: {[argument_index_or_keyword]:[width][.precision][type]}
type can be used with format codes
Following examples will make things more clear.
Example 1:
Here we specify 2 digits of precision and f is used to represent floating point number.
Expected Output:
Example 2:
Here we specify 3 digits of precision, 10 for width and f for floating point number.
Expected Output:
Example 3:
here d in {1:d} represents integer value.
Expected Output:
You need to specify precision only in case of floating point numbers if you specify precision for integer ValueError will be raised.
Example 5:
Expected Output:
Example 6:
Expected Output:
Example 7:
Expected Output:
Example 8:
Expected Output:
format() method also supports keywords arguments.
Note while using keyword arguments we need to use arguments inside {} not numeric index.
You can also mix position arguments with keywords arguments
format() method of formatting string is quite new and was introduced in python 2.6 . There is another old technique you will see in legacy codes which allows you to format string using % operator instead of format() method.
Let’s take an example.
Here we are using template string on the left of % . Instead of {} for format codes we are using % . On the right side of % we use tuple to contain our values. %d and %.2f are called as format specifiers, they begin with % followed by character that represents the data type. For e.g %d format specifier is a placeholder for a integer, similarly %.2f is a placeholder for floating point number.
So %d is replaced by the first value of the tuple i.e 12 and %.2f is replaced by second value i.e 150.87612 .
Expected Output:
Some more examples
Example 1:
New: "{0:d} {1:d} ".format(12, 31)
Old: "%d %d" % (12, 31)
Expected Output:
Example 2:
New: "{0:.2f} {1:.3f}".format(12.3152, 89.65431)
Old "%.2f %.3f" % (12.3152, 89.65431)
Expected Output:
Example 3:
New: "{0:s} {1:o} {2:.2f} {3:d}".format("Hello", 71, 45836.12589, 45 )
Old: "%s %o %.2f %d" % ("Hello", 71, 45836.12589, 45 )
Expected Output:
Syntax: template.format(p1, p1, .... , k1=v1, k2=v2)
template is a string containing format codes, format() method uses it’s argument to substitute value for each format codes. For e.g
{0} and {1} are format codes. The format code {0} is replaced by the first argument of format() i.e 12 , while {1} is replaced by the second argument of format() i.e 31 .
Expected Output:
This technique is okay for simple formatting but what if you want to specify precision in floating point number ? For such thing you need to learn more about format codes. Here is the full syntax of format codes.
Syntax: {[argument_index_or_keyword]:[width][.precision][type]}
type can be used with format codes
Format codes | Description |
---|---|
d | for integers |
f | for floating point numbers |
b | for binary numbers |
o | for octal numbers |
x | for octal hexadecimal numbers |
s | for string |
e | for floating point in exponent format |
Example 1:
Here we specify 2 digits of precision and f is used to represent floating point number.
Expected Output:
Example 2:
Here we specify 3 digits of precision, 10 for width and f for floating point number.
Expected Output:
Example 3:
here d in {1:d} represents integer value.
Expected Output:
You need to specify precision only in case of floating point numbers if you specify precision for integer ValueError will be raised.
Example 5:
Expected Output:
Example 6:
Expected Output:
Example 7:
Expected Output:
Example 8:
Expected Output:
format() method also supports keywords arguments.
Note while using keyword arguments we need to use arguments inside {} not numeric index.
You can also mix position arguments with keywords arguments
format() method of formatting string is quite new and was introduced in python 2.6 . There is another old technique you will see in legacy codes which allows you to format string using % operator instead of format() method.
Let’s take an example.
Here we are using template string on the left of % . Instead of {} for format codes we are using % . On the right side of % we use tuple to contain our values. %d and %.2f are called as format specifiers, they begin with % followed by character that represents the data type. For e.g %d format specifier is a placeholder for a integer, similarly %.2f is a placeholder for floating point number.
So %d is replaced by the first value of the tuple i.e 12 and %.2f is replaced by second value i.e 150.87612 .
Expected Output:
Some more examples
Example 1:
New: "{0:d} {1:d} ".format(12, 31)
Old: "%d %d" % (12, 31)
Expected Output:
Example 2:
New: "{0:.2f} {1:.3f}".format(12.3152, 89.65431)
Old "%.2f %.3f" % (12.3152, 89.65431)
Expected Output:
Example 3:
New: "{0:s} {1:o} {2:.2f} {3:d}".format("Hello", 71, 45836.12589, 45 )
Old: "%s %o %.2f %d" % ("Hello", 71, 45836.12589, 45 )
Expected Output:
Comments
Post a Comment