mirror of
https://github.com/jbranchaud/til
synced 2026-08-31 16:51:46 +00:00
918 B
918 B
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:
> character = ["Gimli", "Dwarf", "Fighter", "Lvl 23"]
=> ["Gimli", "Dwarf", "Fighter", "Lvl 23"]
> character.join(" ~ ")
=> "Gimli ~ Dwarf ~ Fighter ~ Lvl 23"
Notice that I call
join on the
list of strings, passing it the specific separator that I want to use.
Python does it the other way around:
>>> character = ["Gimli", "Dwarf", "Fighter", "Lvl 23"]
>>> " ~ ".join(character)
'Gimli ~ Dwarf ~ Fighter ~ Lvl 23'
The separator is the object that I call
join on, passing
it the list of strings that I want to join.