ToString[expr]
gives a string corresponding to the printed form of expr in OutputForm.
ToString[expr,form]
gives the string corresponding to output in the specified form.
ToString
ToString[expr]
gives a string corresponding to the printed form of expr in OutputForm.
ToString[expr,form]
gives the string corresponding to output in the specified form.
Details and Options
- The following options can be given:
-
CharacterEncoding "Unicode" what raw character encoding to use FormatType OutputForm default format for printing expressions NumberMarks $NumberMarks when to use ` marks in approximate numbers PageWidth Infinity number of character widths per line TotalWidth Infinity maximum number of character widths for a single expression - The default setting CharacterEncoding"Unicode" faithfully preserves all special characters in the input expression.
- ToString uses any relevant definitions given for Format and MakeBoxes. »
Examples
open all close allBasic Examples (2)
Give a string corresponding to the OutputForm of x2:
ToString[x^2]Head[%]Give a string corresponding to the InputForm:
ToString[x^2, InputForm]Scope (2)
Strings for OutputForm, InputForm, StandardForm, and TraditionalForm of an expression:
ToString[x ^ 2 + y ^ 3, OutputForm]ToString[x ^ 2 + y ^ 3, InputForm]ToString[x ^ 2 + y ^ 3, StandardForm]ToString[x ^ 2 + y ^ 3, TraditionalForm]Non-default character encoding:
ToString[α, StandardForm]ToString[α, StandardForm, CharacterEncoding -> "ASCII"]Options (5)
CharacterEncoding (1)
FormatType (1)
Use FormatType to change the formatting:
ToString[Exp[x]]ToString[Exp[x], FormatType -> StandardForm]NumberMarks (1)
Use the default setting of InputForm formatting:
ToString[1.2, InputForm]Explicitly set whether number marks should be included:
{ToString[1.2, InputForm, NumberMarks -> True], ToString[1.2, InputForm, NumberMarks -> False]}PageWidth (1)
TotalWidth (1)
Limit the length of the output string to 50 characters:
ToString[Range[100], InputForm, TotalWidth -> 50]If PageWidth is less than TotalWidth, there might be line breaks introduced:
ToString[Range[100], InputForm, TotalWidth -> 50, PageWidth -> 20]But the string effective length of the string is still less than 50, with the two newlines not counted:
StringLength[%]Applications (2)
Construct an indexed set of variables:
v = ToExpression["x" <> ToString[#]]& /@ Range[5]x1 = 5;vExport data to files with programmatically created names:
v = Map[{#, #[1.]}&, {Sin, Cos, Tan, Cot, Exp}]f[x_] := Export[FileNameJoin[{$TemporaryDirectory, ToString[x[[1]]] <> ".txt"}], x[[2]]]ofiles = f /@ v;StringTake[#, -7]& /@ ofilesImport /@ ofilesProperties & Relations (3)
Use ToExpression to convert strings or boxes to expressions:
ToString[x ^ 3, StandardForm]ToExpression[%]Use ToBoxes to convert expressions to boxes:
ToBoxes[%]Use ToString with ToExpression to convert TeX to Wolfram Language syntax:
t = TeXForm[1.2 x + Exp[y]]ToExpression[ToString[t], TeXForm]ToString uses definitions from Format and MakeBoxes:
ToString[kappa]Format[kappa] := κToString[kappa]ToString[gplus[a, b, c], StandardForm]gplus/:MakeBoxes[gplus[x_, y_, n_], StandardForm] := RowBox[{MakeBoxes[x, StandardForm], SubscriptBox["⊕", MakeBoxes[n, StandardForm]], MakeBoxes[y, StandardForm]}]ToString[gplus[a, b, c], StandardForm]Possible Issues (2)
ToString by default uses OutputForm formatting:
ToString[u + Style[i, Red]]Specify other formats to preserve styling information:
ToString[u + Style[i, Red], StandardForm]ToString[u + Style[i, Red], TraditionalForm]If both PageWidth and TotalWidth are finite, results in StandardForm and TraditionalForm may be unpredictable:
ToString[Range[100], StandardForm, TotalWidth -> 50, PageWidth -> 20]See Also
ToBoxes ToExpression HoldForm WriteString TextString SymbolName StringTemplate
Function Repository: ToFullString
Tech Notes
Related Guides
Related Workflows
- Build a Multipage Form
History
Introduced in 1988 (1.0) | Updated in 1996 (3.0)
Text
Wolfram Research (1988), ToString, Wolfram Language function, https://reference.wolfram.com/language/ref/ToString.html (updated 1996).
CMS
Wolfram Language. 1988. "ToString." Wolfram Language & System Documentation Center. Wolfram Research. Last Modified 1996. https://reference.wolfram.com/language/ref/ToString.html.
APA
Wolfram Language. (1988). ToString. Wolfram Language & System Documentation Center. Retrieved from https://reference.wolfram.com/language/ref/ToString.html
BibTeX
@misc{reference.wolfram_2026_tostring, author="Wolfram Research", title="{ToString}", year="1996", howpublished="\url{https://reference.wolfram.com/language/ref/ToString.html}", note=[Accessed: 13-June-2026]}
BibLaTeX
@online{reference.wolfram_2026_tostring, organization={Wolfram Research}, title={ToString}, year={1996}, url={https://reference.wolfram.com/language/ref/ToString.html}, note=[Accessed: 13-June-2026]}