Integer List printed as String in Elixir

The value you see 'efgh' is not a String but a Charlist.

The result of your statement should be [101, 102, 103, 104] (and it actually is), but it doesn’t output it that way. The four values in your list map to e, f, g and h in ASCII, so iex just prints their codepoints instead of the list. If the list contains invalid characters (such as 0, or 433 like in your case), it leaves it as a simple list.

From Elixir’s Getting Started guide:

A char list contains the code points of the characters between single-quotes (note that by default IEx will only output code points if any of the chars is outside the ASCII range).


Both 'efgh' and [101, 102, 103, 104] are equal in Elixir, and to prove that you can force inspect to print them as a List instead:

[101, 102, 103, 104] == 'efgh'
#=> true

[101, 102, 103, 104] |> inspect(charlists: :as_lists)
#=> [101, 102, 103, 104]

You can also configure IEx to always print charlists as lists:

IEx.configure(inspect: [charlists: :as_lists])

Leave a Comment