Reputation: 839
It seems as if string formatting doesn't work on concatenated strings. With concatenation the place holder gets printed literally:
>>> print("{}" + " OK".format("Text"))
{} OK
However, without concatenation the format gets printed as it should:
>>> print("{} OK".format("Text"))
Text OK
The same problem occurs with old-style %-formatting.
If I have a long multi-line string where I would like to concatenate a string that should be formatted, what is the recommended way?
Upvotes: 0
Views: 230
Reputation: 2677
You were attempting to perform the "format" operation prior to doing the concatenation. You can fix the precedence of operations by using parentheses:
>>> the_string = ("{}" + " OK").format("Text")
>>> print(the_string)
Text OK
Upvotes: 2
Reputation: 402
You just need to fix the parenthesis:
print(("{}" + " OK").format("Text"))
Upvotes: 1