Loop character A/B alternately
<p>How can I print A/B character alternately in python loop?</p>

<p>What I expect in result:</p>

<pre><code>oneA
twoB
threeA
fourB
...
</code></pre>

<p>You can use <a href="https://docs.python.org/3.4/library/itertools.html#itertools.cycle" rel="nofollow"><code>itertools.cycle</code></a> to repeat through a sequence. This is typically used with <a href="https://docs.python.org/3/library/functions.html#zip" rel="nofollow"><code>zip</code></a> to iterate through a longer list, while repeating the shorter one. For example</p>

<pre><code>import itertools
for i,j in zip(['one', 'two', 'three', 'four'], itertools.cycle('AB')):
    print(i+j)
</code></pre>

<p>Output</p>

<pre><code>oneA
twoB
threeA
fourB
</code></pre>

python loops for-loop