I want to pass two parameters to my string, using % s.
I tried this, but it did not work :
title = "im %s with %s"
title % "programming" % "python"
it gives this error :
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: not enough arguments for format string
Do you have an idea?
thanks
The problem is probably best understood by breaking down how these formatting directives work. The basic idea is that each
%in a string means an argument needs to be subsequently supplied to the string.For instance, this would work:
and yield
The ‘s’ in
%smeans that this is a place holder for a string. A ‘d’ would be for integers, ‘f’ for floats etc. There are also additional parameters you can specify. See these docs.If you don’t supply enough items for each place holder, it will result in the
not enough arguments for format stringmessage.Your specific example first creates a string constant that has two formatting directives in it. Then when you use it, you’ll have to supply two items for it.
In other words,
becomes