Consider the strings
string_0 = '%s test'
string_1 = '{} test'
Imagine now the desired output is to return the formatted string using the variable x = 'A'
.
For the first case there is an easy and elegant solution:
string_0 %= x
print(string_0)
# A test
Is there something similar for the second case? E.g.
string_1 f= x # Unfortunately does not work
print(string_1)
# A test
Addressing the comments/responses
- I realise
string_1
is not an f-string but Python won't allow f-strings with empty expression - I know
f'{x} test'
works perfectly well but requires knowingx
before the creation. - The
format
solutions is also something I'm familiar with but when comparings = s.format(x)
tos %= x
I find not very smooth
PS Anticipating the responses, I wanted to edit the above part in my question but was not able due to the simultaneous edit.