I am currently trying to replace the occurrence of a number x with the xth argument in a string. Here is my code:
def simple_format(format, *args):
res = format.replace("%", "")
for x in res:
if (isinstance(x, int)):
res = res.replace(x, args(x))
return res
The first replace is used to remove the occurrence of %
in the string and the next is to check if the variable encountered is a number and if it is, replacing it with the argument.
For example,
simple_format("hello %0", "world", "Ben")
should return "hello world"
.The desired output is not achieved. What is the mistake? Is there any way to do it using replace method?
Thanks in advance.