From a16dcc7760dd1d6a3da4a9094894dc995eb63e39 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Tue, 18 Aug 2026 21:42:31 -0500 Subject: [PATCH] Add Join A List Of Strings as a Python TIL --- README.md | 3 ++- python/join-a-list-of-strings.md | 30 ++++++++++++++++++++++++++++++ 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 python/join-a-list-of-strings.md diff --git a/README.md b/README.md index c9d86e7..9174323 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/python/join-a-list-of-strings.md b/python/join-a-list-of-strings.md new file mode 100644 index 0000000..fbb956d --- /dev/null +++ b/python/join-a-list-of-strings.md @@ -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.