Create an account


Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tut] Python Join List with Underscore [The Most Pythonic Way]

#1
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/...honic-way/
Reply



Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tut] The Most Pythonic Way to Get N Largest and Smallest List Elements xSicKxBot 0 920 09-01-2023, 03:23 AM
Last Post: xSicKxBot
  [Tut] List Comprehension in Python xSicKxBot 0 868 08-23-2023, 07:54 PM
Last Post: xSicKxBot
  [Tut] Collections.Counter: How to Count List Elements (Python) xSicKxBot 0 839 08-19-2023, 06:03 AM
Last Post: xSicKxBot
  [Tut] 5 Effective Methods to Sort a List of String Numbers Numerically in Python xSicKxBot 0 670 08-16-2023, 08:49 AM
Last Post: xSicKxBot
  [Tut] Sort a List, String, Tuple in Python (sort, sorted) xSicKxBot 0 780 08-15-2023, 02:08 PM
Last Post: xSicKxBot
  [Tut] Python Converting List of Strings to * [Ultimate Guide] xSicKxBot 0 642 05-02-2023, 01:17 PM
Last Post: xSicKxBot
  [Tut] Python List of Tuples to DataFrame ? xSicKxBot 0 659 04-22-2023, 06:10 AM
Last Post: xSicKxBot
  [Tut] Python List of Dicts to Pandas DataFrame xSicKxBot 0 728 04-11-2023, 04:15 AM
Last Post: xSicKxBot
  [Tut] Python | Split String into List of Substrings xSicKxBot 0 640 12-11-2022, 12:17 PM
Last Post: xSicKxBot
  [Tut] Python Find in List [Ultimate Guide] xSicKxBot 0 696 12-09-2022, 11:35 PM
Last Post: xSicKxBot

Forum Jump:


Users browsing this thread:
1 Guest(s)

Forum software by © MyBB Theme © iAndrew 2016