[erlang-questions] How to get strings instead of ints out of this code?

Per Hedeland <>
Wed May 7 22:59:31 CEST 2008

Lev Walkin <> wrote:
>Jarrod Roberson wrote:
>> Erlang (BEAM) emulator version 5.6 [smp:2] [async-threads:0]
>> Eshell V5.6  (abort with ^G)
>> 1> [A,B|T] = "abcdefg".
>> "abcdefg"
>> 2> A.
>> 97
>> 3> B.
>> 98
>> 4> T.
>> "cdefg"
>> I know _why_ A. and B. print out as ints and why T. prints out as a 
>> string of characters, how do I get A and B to print out as characters?
>Try [A]. There's no such thing as an individual character in Erlang.

Yes there is, just as much as there is in, say, C... A character is just
an integer, you have to interpret it as a character code if that is what
you want.

If you want to print some data in Erlang, you use one of the io(3)
functions, typically format/2 (or /3), and in doing so you get to
specify how that data is to be interpreted. The shell is no different -
some people seem to think that the output the shell generates is somehow
the "true value" of the expression that was evaluated, but it's just
using the ~p "control sequence", which tries to "detect strings" but
otherwise just prints the value "prettily" in standard syntax:

1> A=$a.
2> io:format("~p~n", [A]).

So, if you give ~p an integer, it prints it as an integer. If you want
to print a character, the control sequence to use is ~c:

3> io:format("~c~n", [A]).

--Per Hedeland

More information about the erlang-questions mailing list