Re format a python string after splitting
<p>I have string like "week32_Aug_24_2016".
I want to change this string like "week32_2016_Aug_24"
I have tried this. </p>

<pre><code>str = "week32_Aug_24_2016"
wk = str.split('_')
newstr = wk[0]+" "+wk[3]+" "+wk[1]+" "+wk[2]
</code></pre>

<p>My expected output is "week32 2016 Aug 24".
I already got that but I want to know is there any better way to do this. Suppose I have long string and no of split value is 10, then this is very long way. So I want to know a better way to arrange the split values. Thanks.... </p>

<p>You can make use of both <a href="https://docs.python.org/3/library/stdtypes.html#str.split" rel="nofollow"><code>str.split()</code></a> and <a href="https://docs.python.org/3/library/stdtypes.html#str.join" rel="nofollow"><code>str.join()</code></a>, simply like this:</p>

<pre><code>string = "week32_Aug_24_2016"
order = (0, 3, 1, 2)
parts = string.split('_')

new_string = ' '.join(parts[i] for i in order)
</code></pre>

<p>Also, note that I renamed your variable <code>str</code> to <code>string</code> to avoid shadowing built-in <a href="https://docs.python.org/3/library/stdtypes.html#str" rel="nofollow"><code>str</code></a> class.</p>

python