cilleroares
(Jesús Manuel Cillero Ares)
December 10, 2023, 11:32am
1
Why when converting a text string started with “1” to an expression with ToExpression, the output is an expression without the “1”?
Example:
In[1406]:= {ToExpression["Wc1F1C1"], ToExpression["1Wc1F1C1"],
ToExpression["2Wc1F1C1"]}
Out[1406]= {Wc1F1C1, Wc1F1C1, 2 Wc1F1C1}
If instead of “1” it is another number, the same does not happen.
Thank you.
fkampas
(Frank Kampas)
December 10, 2023, 3:12pm
2
1 multiplied by an expression is just the expression
In[2]:= 1 a
Out[2]= a
fkampas
(Frank Kampas)
December 10, 2023, 7:49pm
3
the one is part of the name
In[7]:= a1
Out[7]= a1
cilleroares
(Jesús Manuel Cillero Ares)
December 10, 2023, 8:53pm
4
Ok,
But the last “1” of the string “Wc1F1C1” does not multiply it: it remains after applying ToExpression.
In[1425]:= {ToExpression["Wc1F1C1"], ToExpression["1Wc1F1C1"],
ToExpression["2Wc1F1C1"], 2 ToExpression["2Wc1F1C1"],
ToExpression["2Wc1F1C1"] 2, ToExpression["2Wc1F1C1"]*2}
Out[1425]= {Wc1F1C1, Wc1F1C1, 2 Wc1F1C1, 4 Wc1F1C1, 4 Wc1F1C1,
4 Wc1F1C1}
Thanks.
The point is that a name for a symbol cannot start with a digit. This is described in the documentation for “Symbol” / “Details”:
The string “name” in Symbol[“name”] must be an appropriate name for a symbol. It can contain any letters, letter‐like forms, or digits, but cannot start with a digit.
You can also see that effect when trying to input a symbol name e.g. as 5xxx. The 5 immediately becomes a multiplier.
It is basically the same reason why
ToExpression["1+1"]
gives 2, and not 1+1. Here is a workaround,if you really need it:
Block[{Times = Inactive[Times]},
ToExpression["1Wc1F1C1"]]