# Convert string to executable code

This is what I have done to turn the string to executable code.

st = "prime[m_]:=Module[{i=2},\[IndentingNewLine]For[,i<m,i++,If[Mod[\
m,i]\[Equal]0,Print[ToString@m<>" is not a Prime "];Break[]]];\
\[IndentingNewLine]If[i\[Equal]m,Print[ToString@m<>" is a Prime "]]\
\[IndentingNewLine]]"
ToExpression[st]
prime[29]
prime[30]


However, no success.

You need to escape the internal quotation marks like so:

st = "prime[m_]:=Module[{i=2},\[IndentingNewLine]For[,i<m,i++,If[Mod[\
m,i]\[Equal]0,Print[ToString@m<>\" is not a Prime \"];Break[]]];\
\[IndentingNewLine]If[i\[Equal]m,Print[ToString@m<>\" is a Prime \"]]\
\[IndentingNewLine]]";
ToExpression[st]
prime[29]
prime[30]

29 is a Prime

30 is not a Prime

• There is a warning when I copied your code. Warning: comma encountered with no adjacent expression. The expression will be treated as Null. – kile Nov 12 '20 at 4:39
• @kile That comes from your code. Namely, For[,i<m..... – Tim Laska Nov 12 '20 at 4:56