I want to create a list of length n
with the letters of the alphabet.
For example, if n = 3
, by list would be {a,b,c}
. If n=6
, the list would be {a,b,c,d,e,f}
.
Is there any easy way to do this?
Mathematica Stack Exchange is a question and answer site for users of Wolfram Mathematica. It only takes a minute to sign up.
Sign up to join this communityNo need to use *CharacterCode[]
:
letters[n_] := CharacterRange["a", "z"][[;; n]]
letters[3]
(*
{"a", "b", "c"}
*)
Nowadays, one can use Alphabet[]
along with Take[]
or Part[]
+ Span[]
:
Take[Alphabet[], 6]
{"a", "b", "c", "d", "e", "f"}
Take[Alphabet[], {19, UpTo[28]}]
{"s", "t", "u", "v", "w", "x", "y", "z"}
In addition to this, Alphabet[]
is aware of alphabets other than the Latin alphabet:
Take[Alphabet["Greek"], 5]
{"α", "β", "γ", "δ", "ε"}
Take[Alphabet["Hebrew"], {4, 8}]
{"ד", "ה", "ו", "ז", "ח"}
lttrsF = FromCharacterCode /@ (ToCharacterCode[#][[1]] + Range[0, #2-1]) &
lttrsF["b", 4]
(* {"b", "c", "d", "e"} *)
You might want to use FromCharacterCode
:
FromCharacterCode[n]
:gives a string consisting of the character with integer code n.
letters[n_] := StringSplit[FromCharacterCode[Range[97, 97 + n - 1]], ""]
letters@3
{"a", "b", "c"}