I want to put tickers in brackets
<p>I want to put AEM into brackets, so that text will look like: Agnico Eagle Mines Limited (AEM)</p>

<pre><code>text = "Agnico Eagle Mines Limited AEM"
def add_brackets(test):
     for word in test:
         if word.isupper():
             word = "(" + word + ")"
     print(test)

print(add_brackets(text))
</code></pre>

<p>What's wrong with the code? I get the original text. </p>

<p>This would be pretty concise with a regular expression substitution:</p>

<pre><code>&gt;&gt;&gt; import re
&gt;&gt;&gt; text = "Agnico Eagle Mines Limited AEM"
&gt;&gt;&gt; re.sub(r'\b([A-Z]+)\b', r'(\1)', text)
'Agnico Eagle Mines Limited (AEM)'
</code></pre>

<p>This looks for multiple uppercase characters together, with word boundaries (like whitespace) on either side, then substitutes that matched group with the same text (the <code>\1</code>) with the addition of parentheses.</p>

<p>In a function:</p>

<pre><code>&gt;&gt;&gt; import re
&gt;&gt;&gt; def add_brackets(s):
...     return re.sub(r'\b([A-Z]+)\b', r'(\1)', s)
...
&gt;&gt;&gt; print(add_brackets(text))
Agnico Eagle Mines Limited (AEM)
</code></pre>

python