Want to share your content on python-bloggers? click here.
I have recently been on a tear of different challenges on the site HackerRank. I am about halfway through their 30 days of code and 10 days of statistics. These challenges often require to output number to a certain a number of significant digits. I’ve always thought that the round
function can be used for this, but I am wrong. The F string seems to be a powerful tool to accomplish this, and worth your time learning if you are unfamiliar.
Structure of an F string
The formatting of an F string starts with a f
prior to quotations, whether they be single or double quotes. Any variable can then be included within a series of {}
. This formatting can make it easier than turning values into strings and concatenating all strings into a single line of text. This is easily demonstrated with a large mix of values and strings.
x = 1/3 y = 1/6 print("The value is " + str(x) + " is greater than " + str(y)) print(f"The value is {x} is greater than {y}")
The value is 0.3333333333333333 is greater than 0.16666666666666666 The value is 0.3333333333333333 is greater than 0.16666666666666666
The values can then be formatted with :
after the variable name. The number of digits prior and post the decimal can then be specified. The f
is added after the decimal formatting to ensure the value is treated as a float.
print(f"The value is {x:.3f} is greater than {y:.2f}")
The value is 0.333 is greater than 0.17
The values passed are not specific to the number of digits, but the minimum number of spaces. This means you can ensure specific space aligned, such as for a table, by including these values.
z = [10000, 500, 10, 0.001, .1] for i in z: print(f"the value is: {i:5}")
the value is: 10000 the value is: 500 the value is: 10 the value is: 0.001 the value is: 0.1
Additionally, we can add leading zeros by adding zero prior to the number of digits.
for i in z: print(f"the value is: {i:05}")
the value is: 10000 the value is: 00500 the value is: 00010 the value is: 0.001 the value is: 000.1
Alternative formatting
There are a few alternative methods for f strings. From my understanding, they are not as fast when it comes to performance. I don’t think that is of particular importance. If your script needs a high level of performance, than you probably don’t want many print statements.
Format Method()
The format
method is very similar to f strings
with the use of the {}
. The string is not preceded by f and the {}
can remain empty or contain position indexing. The values are then added in the .format
function after the string. The order of the variable in the string will correspond with the number used in the {}
, if used at all.
print("The value is {} is greater than {}".format(1/3, 1/6))
The value is 0.3333333333333333 is greater than 0.16666666666666666
Old % Method
The Old % operator (modulo) replaces the value in the string. Formatting details, such as those previously discussed, are entered after the %
. The variables or values are then entered after the string when preceded by another %
. Multiple values can be passed.
print("The value is %5.3f is greater than %5.3f" %(x,y))
The value is 0.333 is greater than 0.167
Conclusions
Whichever method you decide, it probably won’t make a huge difference. The important part is to understand is the actual formatting. F strings also seem to make it easier to understand the code, as the actual values are inline with the string and the formatting.
Want to share your content on python-bloggers? click here.