Concatenate string using .format
<p>I have some code similar to the following:</p>

<pre><code>test_1 = 'bob'
test_2 = 'jeff'

test_1 += "-" + test_2 + "\n"
</code></pre>

<p>Output:</p>

<pre><code>bob- jeff\n
</code></pre>

<p>I'd like to have the same functionality but using the <code>.format</code> method.</p>

<p>This is what I have so far:</p>

<pre><code>test_1 = "{}{} {}\n".format(test_1, "-", test_2)
</code></pre>

<p>Which produces the same output, but is there a better/more efficient way of using <code>.format.</code> in this case?</p>

<p><code>''.join</code> is probably fast enough and efficient.</p>

<pre><code>'-'.join((test_1,test_2))  
</code></pre>

<p>You can measure different methods using the <code>timeit</code> module. That can tell you which is fastest  </p>

<p>This is an example of how <code>timeit</code> can be used:-</p>

<pre><code>&gt;&gt;&gt; import timeit
&gt;&gt;&gt; timeit.timeit('"-".join(str(n) for n in range(100))', number=10000)
0.8187260627746582
</code></pre>

python string python-2.7 string-formatting string-concatenation