Add Join A List Of Strings as a Python TIL

This commit is contained in:
jbranchaud
2026-08-18 21:42:31 -05:00
parent c495194a0d
commit a16dcc7760
2 changed files with 32 additions and 1 deletions
+2 -1
View File
@@ -10,7 +10,7 @@ working across different projects via [VisualMode](https://www.visualmode.dev/).
For a steady stream of TILs, [sign up for my newsletter](https://visualmode.kit.com/newsletter).
_1865 TILs and counting..._
_1866 TILs and counting..._
See some of the other learning resources I work on:
@@ -1110,6 +1110,7 @@ If you've learned something here, support my efforts writing daily TILs by
- [Install With PIP For Specific Interpreter](python/install-with-pip-for-specific-interpreter.md)
- [Iterate First N Items From Enumerable](python/iterate-first-n-items-from-enumerable.md)
- [Iterate Over A Dictionary](python/iterate-over-a-dictionary.md)
- [Join A List Of Strings](python/join-a-list-of-strings.md)
- [Keep A Tally With collections.Counter](python/keep-a-tally-with-collections-counter.md)
- [Lint And Format Project With Ruff](python/lint-and-format-project-with-ruff.md)
- [Load A File Into The Python REPL](python/load-a-file-into-the-python-repl.md)
+30
View File
@@ -0,0 +1,30 @@
# Join A List Of Strings
Though joining a list of strings in Python is a basic task, I wanted to write
about it because it is backward from how it is done in Ruby (which trips me up
every single time).
So, in Ruby I would do the following:
```ruby
> character = ["Gimli", "Dwarf", "Fighter", "Lvl 23"]
=> ["Gimli", "Dwarf", "Fighter", "Lvl 23"]
> character.join(" ~ ")
=> "Gimli ~ Dwarf ~ Fighter ~ Lvl 23"
```
Notice that I call
[`join`](https://docs.ruby-lang.org/en/master/Array.html#method-i-join) on the
list of strings, passing it the specific separator that I want to use.
Python does it the other way around:
```python
>>> character = ["Gimli", "Dwarf", "Fighter", "Lvl 23"]
>>> " ~ ".join(character)
'Gimli ~ Dwarf ~ Fighter ~ Lvl 23'
```
The separator is the object that I call
[`join`](https://docs.python.org/3/library/stdtypes.html#str.join) on, passing
it the list of strings that I want to join.