Python format() without {}
<p>I am working on python 3.6 64 bit.</p>

<p>Here is my code:</p>

<pre><code>days = "Mon Tue Wed Thu Fri Sat Sun"

print("Here are the days",format(days))
</code></pre>

<p>The output I got is </p>

<p>Here are the days Mon Tue Wed Thu Fri Sat Sun</p>

<p>I didn't add "{}" in my string. Also I used a comma "," instead of a dot "."</p>

<p>My understanding was that format() will replace {} in string with its arguments.</p>

<p>Question : How did format() worked without {} and . operator</p>

<p>Some of the confusion here may be the difference between the <code>format()</code> <em>function</em> and the related <code>"string".format()</code> <em>method</em>.  The function invokes the object's (first argument) <code>__format__()</code> method with a <em>format specification</em> (second argument).  If the second argument is None, then it's just like <code>str()</code>.  The <code>format()</code> method of <code>str</code> walks the string on which it is invoked, looking for <em>substitution brackets</em> which may, or may not contain format specifications to be applied similar to the <code>format()</code> function.  An example:</p>

<pre><code>&gt;&gt;&gt; # format() function which invokes float.__format__()
&gt;&gt;&gt; format(0.324, "+.4%")
'+32.4000%'
&gt;&gt;&gt;
&gt;&gt;&gt; # str format() method that invokes various object.__format__() methods
&gt;&gt;&gt; # if a format specification is found in the substitution brackets:
&gt;&gt;&gt; "{:+.4%} rise in {}".format(0.324, "temperature")
'+32.4000% rise in temperature'
&gt;&gt;&gt; 
</code></pre>

<blockquote>
  <p>Question : How did format() worked without {} and . operator</p>
</blockquote>

<p>It worked because without the '.' operator, you mistakenly invoked the <code>format()</code> function which, with no second argument, simply return the string.  The <code>format()</code> function doesn't use {} brackets.  It's the <code>format()</code> method of str, invoked by the '.' operator, that uses {} brackets to make substitutions, some of which may require formatting.</p>

<p>Perhaps it might have been less confusing if the str method had a different name like <code>"{} string {}".interpolate(x, y)</code> as formatting is only part of what it does.</p>

python format