Skip to content

Latest commit

 

History

History
30 lines (23 loc) · 918 Bytes

File metadata and controls

30 lines (23 loc) · 918 Bytes

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.