What is the easiest way to get a list of the keywords in a string?
<p>For example:</p>

<pre><code>str = 'abc{text}ghi{num}'
</code></pre>

<p>I can then do</p>

<pre><code>print(str.format(text='def',num=5))
&gt; abcdefghi5
</code></pre>

<p>I would like to do something like</p>

<pre><code>print(str.keywords) # function does not exist
&gt; ['text','num']
</code></pre>

<p>What is the easiest way to do this? I can search character-by-character for <code>{</code> and <code>}</code> but I wonder if there a built-in python function?</p>

<p>Thank you.</p>

<p>Check out the <a href="https://docs.python.org/3.5/library/string.html#string.Formatter" rel="nofollow"><code>string.Formatter</code> class</a>:</p>

<pre><code>&gt;&gt;&gt; import string
&gt;&gt;&gt; text = 'abc{text}ghi{num}'
&gt;&gt;&gt; [t[1] for t in string.Formatter().parse(text)]
['text', 'num']
</code></pre>

python string python-3.x formatting