Search code examples
pythonpython-3.xtemplatesstring-formattingf-string

Store formatted strings, pass in values later?


I have a dictionary with a lot of strings.

Is it possible to store a formatted string with placeholders and pass in a actual values later?

I'm thinking of something like this:

d = {
  "message": f"Hi There, {0}"
}

print(d["message"].format("Dave"))

The above code obviously doesn't work but I'm looking for something similar.


Solution

  • You use f-string; it already interpolated 0 in there. You might want to remove f there

    d = {
              # no f here
      "message": "Hi There, {0}"
    }
    
    print(d["message"].format("Dave"))
    
    Hi There, Dave