I want to understand what value_in_dictionary % dictionary
does.
I tried
values = {
'hello':'world',
'hello2':'world2',
}
value = 'world'
print(value % values)
This prints
world
For strings, modulo triggers old style string interpolation. The right hand operand can be a scalar, tuple or dictionary.
The documentation reads as follows:
When the right argument is a dictionary (or other mapping type), then the formats in the string must include a parenthesised mapping key into that dictionary inserted immediately after the
'%'
character. The mapping key selects the value to be formatted from the mapping. For example:>>> print('%(language)s has %(number)03d quote types.' % ... {'language': "Python", "number": 2}) Python has 002 quote types.
Your particular example has no formats in it, so nothing gets interpolated. Adding a valid interpolation key like world = 'hello %(hello)'
or world = 'hello %(hello2)'
would illustrate how it works. Attempting world = 'hello %(hello3)'
would result in a KeyError
.