Reputation: 5723
I am quite familiar with python3 formatting options but I haven't faced a {0!s}
option before.
In particular the format I encountered was something like:
'{0!s}.xml'.format('VID/' + file)
which as far as I get it it's just a way to render the output of str(arg)
and in my case str('VID/')
according to this explanation.
So the output would be something like:
VID/000.xml
for example.
Since my argument is already a str
in this case I did not see any difference when I just omit the whole formatting parameter (use {}
instead of {0!s}
).
Other experiments I made was omitting the leading 0 which produces the same result, while replacing it with another number like 1 produces an error:
IndexError: tuple index out of range
Can someone explain what's the case with this formatting parameter? When should I use it etc?
Upvotes: 0
Views: 177
Reputation: 21285
It's value conversion: https://pyformat.info/#conversion_flags
{0!s}.xml
Means "For the 0th argument, get the string to to put in the formatted string by calling the __str__
method"
You can also use {0!r}
to call the __repr__
method instead
Upvotes: 1