Skip to main content
Post Made Community Wiki by J. M.'s missing motivation
Source Link

This is a common trap with using StringForm[]. But before discussing the pitfall, let me offer slightly simpler code:

With[{expr = p[0, 1, 2]},
     StringForm["`1``2`", Head[expr], StringJoin[ToString /@ (List @@ expr)]]]
   p012

Looks good so far, but let's see what InputForm[] says:

InputForm[%]
   StringForm["`1``2`", p, "012"]

Certainly not a string!

So, you need an additional ToString[]:

ToString[%] // InputForm
   "p012"

The alternative in newer versions of Mathematica is StringTemplate[], which produces a genuine string. Observe:

With[{expr = p[0, 1, 2]},
     StringTemplate["`head``args`"] @
     <|"head" -> Head[expr], "args" -> StringJoin[ToString /@ (List @@ expr)]|>]
   "p012"