Sick Gaming
[Tut] Python Join List with Underscore [The Most Pythonic Way] - Printable Version

+- Sick Gaming (https://www.sickgaming.net)
+-- Forum: Programming (https://www.sickgaming.net/forum-76.html)
+--- Forum: Python (https://www.sickgaming.net/forum-83.html)
+--- Thread: [Tut] Python Join List with Underscore [The Most Pythonic Way] (/thread-95546.html)



[Tut] Python Join List with Underscore [The Most Pythonic Way] - xSicKxBot - 06-07-2020

Python Join List with Underscore [The Most Pythonic Way]

<div><p class="has-background has-luminous-vivid-amber-background-color"><strong>The <code>'_'.join(list)</code> method on the underscore string <code>'_'</code> glues together all strings in the <code>list</code> using the underscore string as a delimiter—and returns a new string. For example, the expression <code>'_'.join(['a', 'b', 'c'])</code> returns the new string <code>'a_b_c'</code>. </strong></p>
<h2>Definition and Usage</h2>
<p>The <code>string.join(iterable)</code> method joins the string elements in the <code>iterable</code> to a new string by using the <code>string</code> on which it is called as a delimiter. </p>
<p>Here’s a short example:</p>
<pre class="EnlighterJSRAW" data-enlighter-language="generic" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">friends = ['Alice', 'Bob', 'Carl'] # Empty delimiter string ''
print(''.join(friends))
# AliceBobCarl # Delimiter string '_'
print('_'.join(friends))
# Alice_Bob_Carl</pre>
<p>The string elements in the list <code>friends</code> are concatenated using the delimiter string <code>''</code> in the first example and the underscore character <code>'_'</code> in the second example.</p>
<p><strong>Related articles:</strong></p>
<ul>
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/python-join-list/" target="_blank">Python Join List [Ultimate Guide]</a></li>
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/python-list-concatenation-add-vs-inplace-add-vs-extend/" target="_blank">Python Concatenation</a></li>
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/concatenate-lists-in-python/" target="_blank">Python How to Concatenate Lists</a></li>
<li><a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" target="_blank">The Ultimate Guide to Python Lists</a></li>
</ul>
<p><strong>Syntax</strong></p>
<p>You can call this method on each list object in Python. Here’s the syntax:</p>
<p><code><code>string.join(iterable)</code></code></p>
<figure class="wp-block-table is-style-stripes">
<table>
<thead>
<tr>
<th>Argument</th>
<th>Description</th>
</tr>
</thead>
<tbody>
<tr>
<td><code>iterable</code></td>
<td>The elements to be concatenated.</td>
</tr>
</tbody>
</table>
</figure>
<h2>Code Puzzle</h2>
<p>To practice what you’ve learned so far, feel free to solve the following interactive code puzzle:</p>
<p> <iframe src="https://trinket.io/embed/python/21de9b905d" width="100%" height="356" frameborder="0" marginwidth="0" marginheight="0" allowfullscreen></iframe> </p>
<p><em><strong>Exercise</strong>: Guess the output and check if you were right by running the interactive code shell.</em></p>
<h2>Where to Go From Here?</h2>
<p>Enough theory, let’s get some practice!</p>
<p>To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?</p>
<p><strong>Practice projects is how you sharpen your saw in coding!</strong></p>
<p>Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?</p>
<p>Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.</p>
<p>Join my free webinar <a rel="noreferrer noopener" href="https://blog.finxter.com/webinar-freelancer/" target="_blank">“How to Build Your High-Income Skill Python”</a> and watch how I grew my coding business online and how you can, too—from the comfort of your own home.</p>
<p><a href="https://blog.finxter.com/webinar-freelancer/" target="_blank" rel="noreferrer noopener">Join the free webinar now!</a></p>
</div>


https://www.sickgaming.net/blog/2020/06/02/python-join-list-with-underscore-the-most-pythonic-way/