# How to convert a symbol to a string of variable it represent

**URL:** https://discourse.julialang.org/t/how-to-convert-a-symbol-to-a-string-of-variable-it-represent/40846
**Category:** New to Julia
**Created:** [June 6, 2020, 1:23am UTC](https://discourse.julialang.org/t/how-to-convert-a-symbol-to-a-string-of-variable-it-represent/40846 "2020-06-06T01:23:22Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [June 6, 2020, 1:23am UTC](https://discourse.julialang.org/t/how-to-convert-a-symbol-to-a-string-of-variable-it-represent/40846/1 "2020-06-06T01:23:22Z")

</div>

```julia
julia> expr = :a + 3 * :b
:a + 3:b

julia> string(expr)
":a + 3:b"

```

How do I convert an expression with symbols in it to a string representing the variables that the symbol represent?

I want “a + 3 b” instead of “:a + 3 :b”

Oh yeah, my use case. I am trying to do symbolic differentiation

julia\> using Symata

julia\> answer = @sym D(3_x^4,x)  
12_:x^3

julia\> answer = replace(string(answer), “:” =\> “”)

julia\> eval(Meta.parse("func = x → $answer”))  
#3 (generic function with 1 method)

julia\> func(5)  
1500

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [June 6, 2020, 1:26am UTC](https://discourse.julialang.org/t/how-to-convert-a-symbol-to-a-string-of-variable-it-represent/40846/2 "2020-06-06T01:26:33Z")

</div>

> [@StevenSiew](#):
>
> `expr = :a + 3 * :b`

This is not valid Julia code apparently:

```julia-auto
julia> expr = :a + 3 * :b
ERROR: MethodError: no method matching *(::Int64, ::Symbol)
Closest candidates are:
  *(::Any, ::Any, ::Any, ::Any...) at operators.jl:538
  *(::T, ::T) where T<:Union{Int128, Int16, Int32, Int64, Int8, UInt128, UInt16, UInt32, UInt64, UInt8} at int.jl:87
  *(::Union{Int16, Int32, Int64, Int8}, ::BigInt) at gmp.jl:538
  ...
Stacktrace:
 [1] top-level scope at REPL[32]:100:

```

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [June 6, 2020, 1:30am UTC](https://discourse.julialang.org/t/how-to-convert-a-symbol-to-a-string-of-variable-it-represent/40846/3 "2020-06-06T01:30:07Z")

</div>

If you have

```julia
julia> ex = :(a + 3*b)
:(a + 3b)

julia> string(ex)
"a + 3b"

```

then it works.

You might want to look into ModelingToolkit for symbolics:

```julia
julia> using ModelingToolkit

julia> @variables a, b
(a, b)

julia> ex = a + 3b
a + 3b

julia> string(ex)
"a + 3b"

```

---

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [June 6, 2020, 1:32am UTC](https://discourse.julialang.org/t/how-to-convert-a-symbol-to-a-string-of-variable-it-represent/40846/4 "2020-06-06T01:32:54Z")

</div>

> [@dpsanders](#):
>
> `expr = :a + 3 * :b`

It worked in Symata

```julia
julia> using Symata

julia> answer = @sym D(3x^4,x)
12*:x^3

julia> answer
12*:x^3

julia> expr = :a + 3 * :b
:a + 3:b

```

---

<div class="post-metadata">

### Author: ![yuyichao](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yuyichao/32/20_2.png) [@yuyichao](https://discourse.julialang.org/u/yuyichao)
#### Post date: [June 6, 2020, 1:57am UTC](https://discourse.julialang.org/t/how-to-convert-a-symbol-to-a-string-of-variable-it-represent/40846/5 "2020-06-06T01:57:45Z")

</div>

> [@dpsanders](#):
>
> This is not valid Julia code apparently:

It **is** valid julia code as much a `error()` is valid julia code. (edit: and by valid julia code I mean `:a + 3 * :b` is a valid expression)

While you should not use `string` to do anything serious, this is basically the same issue as [Printing of multiplication and vs. UInt16 etc. · Issue #36108 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/36108) which is fixed on master now by [fix #36108, printing invalid numeric juxtapositions by JeffBezanson · Pull Request #36122 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/pull/36122).

> [@StevenSiew](#):
>
> I want “a + 3 b” instead of “:a + 3 :b”

This won’t happen since they aren’t the same expression.

---

<div class="post-metadata">

### Author: ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)
#### Post date: [June 6, 2020, 2:14am UTC](https://discourse.julialang.org/t/how-to-convert-a-symbol-to-a-string-of-variable-it-represent/40846/6 "2020-06-06T02:14:08Z")

</div>

Can you explain what you are trying to do at a higher level? I mean can you explain the problem you are trying to solve, rather than talking about the method you are trying to solve it. It could be a good approach, but there may be other, better approaches.

---

<div class="post-metadata">

### Author: ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)
#### Post date: [June 6, 2020, 2:22am UTC](https://discourse.julialang.org/t/how-to-convert-a-symbol-to-a-string-of-variable-it-represent/40846/7 "2020-06-06T02:22:33Z")

</div>

> [@StevenSiew](#):
>
> answer = @sym D(3 _x^4,x)_

You might want

```julia-auto
julia> @sym answer = D(3x^4, x)
12*:x^3

julia> func = @sym Compile([x], Evaluate(answer))
#3 (generic function with 1 method)

julia> func(5)
1500

```

or

```julia-auto
julia> @sym expr = 3a + b
3:a + :b

julia> func1 = @sym Compile([a, b], Evaluate(expr))
#5 (generic function with 1 method)

julia> func1(1, 2)
5
```

---

<div class="post-metadata">

### Author: ![jlapeyre](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jlapeyre/32/4514_2.png) [@jlapeyre](https://discourse.julialang.org/u/jlapeyre)
#### Post date: [June 6, 2020, 2:51am UTC](https://discourse.julialang.org/t/how-to-convert-a-symbol-to-a-string-of-variable-it-represent/40846/8 "2020-06-06T02:51:37Z")

</div>

> [@StevenSiew](#):
>
> It worked in Symata

This is a bug in Symata. Defining a method that allows `3 * :b` is type piracy. This method was meant to be opt in, rather than on by default.

---

<div class="post-metadata">

### Author: ![StevenSiew](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevensiew/32/218393_2.png) [@StevenSiew](https://discourse.julialang.org/u/StevenSiew)
#### Post date: [June 6, 2020, 5:37am UTC](https://discourse.julialang.org/t/how-to-convert-a-symbol-to-a-string-of-variable-it-represent/40846/9 "2020-06-06T05:37:05Z")

</div>

This is what I was trying to achieve  
Given some Julia code (of the body of a simple function)  
Find the symbolic differentiation of the code  
Then turn it into a function  
To use in Newton-Raphson method  
[https://en.wikipedia.org/wiki/Newton%27s\_method](https://en.wikipedia.org/wiki/Newton%27s_method)

```julia
julia> using Symata

julia> expr = :( 3 * x^4)
:(3 * x ^ 4)

julia> eval(Meta.parse("@sym answer = D($expr,x)"))
12*:x^3

julia> func = @sym Compile([x], Evaluate(answer))
#3 (generic function with 1 method)

julia> func(5)
1500

```

---

<div class="post-metadata">

### Author: ![dpsanders](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dpsanders/32/3573_2.png) [@dpsanders](https://discourse.julialang.org/u/dpsanders)
#### Post date: [June 6, 2020, 8:06am UTC](https://discourse.julialang.org/t/how-to-convert-a-symbol-to-a-string-of-variable-it-represent/40846/10 "2020-06-06T08:06:39Z")

</div>

You don’t need symbolic differentiation for this – you can use automatic differentiation using e.g. [https://github.com/JuliaDiff/ForwardDiff.jl](https://github.com/JuliaDiff/ForwardDiff.jl)

But ModelingToolkit can find and use the symbolic derivative.
