# Expr to String conversion

**URL:** <https://discourse.julialang.org/t/expr-to-string-conversion/4118>\
**Category:** New to Julia\
**Created:** [June 6, 2017, 10:48pm UTC](https://discourse.julialang.org/t/expr-to-string-conversion/4118 "2017-06-06T22:48:18Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![mohamed82008](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mohamed82008/32/18171_2.png) [@mohamed82008](https://discourse.julialang.org/u/mohamed82008)\
**Post date:** [June 6, 2017, 10:48pm UTC](https://discourse.julialang.org/t/expr-to-string-conversion/4118/1 "2017-06-06T22:48:18Z")

</div>

Is there a function that converts an expression or symbol to a String representation?

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [June 6, 2017, 11:06pm UTC](https://discourse.julialang.org/t/expr-to-string-conversion/4118/2 "2017-06-06T23:06:21Z")

</div>

`repr`

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [June 7, 2017, 5:45am UTC](https://discourse.julialang.org/t/expr-to-string-conversion/4118/3 "2017-06-07T05:45:38Z")

</div>

`repr` in julia does not promise to do this.  
I think it should.  
But it does not.

> help?\> repr  
> **repr(x)**  
> Create a string from any value using the showall function.
> 
> help?\> showall  
> **showall(x)**  
> Similar to show, except shows all elements of arrays.
> 
> help?\> show  
> **show(stream, mime, x)**  
> The display functions ultimately call show in order to write an object x as a given mime type to a given I/O stream (usually a memory buffer), if possible. In order to  
> provide a rich multimedia representation of a user-defined type T, it is only necessary to define a new show method f  
> …  
> The default MIME type is MIME"text/plain". There is a fallback definition for text/plain output that calls show with 2 arguments. Therefore, this case should be  
> handled by defining a 2-argument show(stream::IO, x::MyType) method.  
> …  
> **show(x)**

> Write an informative text representation of a value to the current output stream. New types should overload show(io, x) where the first argument is a stream. The  
> representation used by show generally includes Julia-specific formatting and type information.

By docs,  
we have an indirect statement that  
the results of `repr` will be similar to those of a function that says that it will return a _“representation … [that] …generally includes Julia-specific formatting and type information.”_

Which is a pretty weak promise, vs: `eval(parse(repr(x)) == x`  
Which would be a nicely strong statement.  
But has its own issues like requiring equality to be defined for all types with `repr` defined.  
And its own issues for objects of types that can not be trivially duplicated (eg file handles).

A weaker statement might be something like:  
`x->eval(parse(repr(x)))` shall be functionally equivalent to `deepcopy`.

Basically as far as julia’s docs for `repr` are concerned,  
`repr` probably gives you an in informative string.  
By a stretch of meaning one could interpret “julia specific formatting” as “parsable”.  
In practice it tends to follow the promise I suggested.

Contrast [Python :](https://docs.python.org/3/library/functions.html#repr)

> repr(object)  
> Return a string containing a printable representation of an object. For many types, this function makes an attempt to return a string that would yield an object with the same value when passed to eval(), otherwise the representation is a string enclosed in angle brackets that contains the name of the type of the object together with additional information often including the name and address of the object. A class can control what this function returns for its instances by defining a **repr** () method.

Very clear as to what it does. Does have some wiggle with saying “for many types”,  
but for types following the convention then they either return an `eval` able string that gives a equal object,  
or if they can not, then the string they give back is wrapped in angle brrackeds and contains the type name and some additional information.

---

<div class="post-metadata">

**Author:** ![david\_streader](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/david_streader/32/37027_2.png) [@david\_streader](https://discourse.julialang.org/u/david_streader)\
**Post date:** [June 8, 2022, 9:13pm UTC](https://discourse.julialang.org/t/expr-to-string-conversion/4118/4 "2022-06-08T21:13:24Z")

</div>

I am also trying to convert an Expr to a string that can be parsed back to the Expr later.  
I stumbled on the fact that string works better that repr (no guarantee that it will always work)  
Meta.parse(string(:(x+4)))  
:(x+4)  
I am very far from an expert and am sure that I am trying to reinvent the wheel. But I need to store Expr both for later evaluation and for user editing. So any better solutions much appreciated. david

---

<div class="post-metadata">

**Author:** ![xor0110](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xor0110/32/7926_2.png) [@xor0110](https://discourse.julialang.org/u/xor0110)\
**Post date:** [January 10, 2024, 7:57am UTC](https://discourse.julialang.org/t/expr-to-string-conversion/4118/5 "2024-01-10T07:57:45Z")

</div>

Interesting alternative, `Base.show_unquoted`: [Way to get string representation of an Expression? - #7 by ForceBru](https://discourse.julialang.org/t/way-to-get-string-representation-of-an-expression/7778/7)
