To achieve this you need to evaluate the "f" <> ToString[i]
expression before Set
(=
) sees it, or you attempt to assign to an object with head StringJoin
, just as the error message tries to tell you. Further, you cannot make an assignment to a string, so you need to convert it to a Symbol, using (surprise) Symbol
. One method is to use Evaluate
:
Do[Evaluate[Symbol["f" <> ToString[i]]] = i*2, {i, 1, 20}]
{f1, f2, f17}
{2, 4, 34}
This however is not usually a good course of action with Mathematica. For example, if any of these symbols already exist and have a value assigned the operation will fail. One can get around this with more effort as seen in the answers to How to Block Symbols without evaluating them? (or more specifically in my answer here) but again, it is not usually a good course of action.
The normal method is to use indexed objects as PrinceBilliard shows.
Please see this question, its answers, and the four related questions linked in the comment below it for more on this topic in general.