In Python, it’s possible to perform mathematical operations inside of string literals, using a feature called “f-strings”. F-strings, or “formatted string literals”, allow you to embed expressions inside string literals, which are evaluated at runtime. This allows you to mix variables, arithmetic expressions, and other computations into strings in a very readable and convenient way.
num_val = 42
print(f'{num_val % 2 = }') # num_val % 2 = 0
Here, the f-string '{num_val % 2 = }'
is a string literal that contains an expression num_val % 2
. The %
operator is used to perform the modulo operation, which returns the remainder of the division of one number by another. In this case, num_val % 2
returns the remainder of the division of 42
by 2
, which is 0
.