Python list to string spacing
<p>I have a list such as this</p>

<pre><code>list = ['Hi', ',', 'my', 'name', 'is', 'Bob', '!']
</code></pre>

<p>I wanted to convert this to a string, and originally, I found on stackoverflow that .join() could be used. So i did:</p>

<pre><code>x = ' '.join(list)
print(x)
</code></pre>

<p>which prints</p>

<pre><code>"Hi , my name is Bob !"
</code></pre>

<p>when what I want printed is:</p>

<pre><code>"Hi, my name is Bob!"
</code></pre>

<p>How do I not add spaces before periods and exclamation points? I want a more general case so that I can for example read in a text file as a list, and convert it to a string.</p>

<p>Thanks!</p>

<p>A little different solution:</p>

<pre><code>&gt;&gt;&gt; from string import punctuation
&gt;&gt;&gt; lis = ["Hi", ",", "my", "name", "is", "Bob", "!"]
&gt;&gt;&gt; string = ''
&gt;&gt;&gt; for i, x in enumerate(lis):
        if x not in punctuation and i != 0:
            string += ' ' + x
        elif x not in punctuation and i == 0:
            string += x
        else:
            string += x
&gt;&gt;&gt; print(string)
"Hi, my name is Bob!"
</code></pre>

python string list