Is there a way to dynamically format a string with string.format()
, when the arguments can vary? I want to write a function where the inputs are a SQL string and a dictionary of name-value pairs; both inputs can vary.
So for example, a SQL string might be
"select * from students where school={schoolArg} and age={ageArg}"
with the dictionary being {schoolArg: "some school", ageArg: 10}
Another SQL string might be
"select * from teachers where class={classArg}"
with the dictionary being {classArg: "history"}
...and so on.
I can use string.replace()
without problems. But is it possible to use string.format()
, where the arguments is not known in advance?