M
M
make_install2013-11-22 00:19:59
Python
make_install, 2013-11-22 00:19:59

Python_How does the format expression work in the example?

Good day, gentlemen!
Help, please, to understand the logic of the formatting expression. I wrote something, but I can’t figure out why the expression works this way. In short, the task was to derive the value of a constant with a given precision.

direct = {'pi':math.pi,'e':math.e,'fi':1.618033988749} # примерный словарь
const = raw_input('enter constant (' example - NAME:ACCUR): ').split(':')  # вывод const - ['fi', '4']
accur = const[1] # достаем необходимую нам точность из списка ввода пользователя
filtr = "%%s = %%.%sf" % (accur,) # после работы выражения получаем %s = %.4f
print filtr % (const[0],direct[const[0]]) 
# после работы выражения получаем fi = 1.6180

Why is the filtr = "%%s = %%.%sf" % (accur,)variable in the expression accursubstituted by default for the second template %%.%sfand not for the first one? It doesn't even matter if you set accur to be passed through a tuple or through a single value.
Forgive me if I wrote chaotically. I am not interested in other ways and methods of solving, I am only interested in this feature of the formatting expression in the given example.
Thanks in advance!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vitaly, 2013-11-22
@make_install

In this expression, a single % character is the indicator of the beginning of the specifier. The double %% is just the percent sign %. The string is processed only once. That is, the line handler turns %%s into %s and continues down the line.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question