# String as a variable name

**URL:** https://discourse.julialang.org/t/string-as-a-variable-name/2195
**Category:** General Usage
**Created:** [February 21, 2017, 1:43am UTC](https://discourse.julialang.org/t/string-as-a-variable-name/2195 "2017-02-21T01:43:29Z")
**Posts on this page:** 8
**Page:** 2

<div class="post-metadata">

### Author: ![Shuhua](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/shuhua/32/27618_2.png) [@Shuhua](https://discourse.julialang.org/u/Shuhua)
#### Post date: [July 24, 2020, 5:32am UTC](https://discourse.julialang.org/t/string-as-a-variable-name/2195/21 "2020-07-24T05:32:31Z")

</div>

From this [StackOverflow thread](https://stackoverflow.com/questions/50084877/how-to-alias-a-macro-in-julia), in Julia 1.3 there is convenient syntax `var"name"` to evaluate a _name string_ into a variable. The documentation is [here](https://docs.julialang.org/en/v1/base/base/#var%22name%22) (thanks to @Tamas_Papp). See examples below with Julia 1.4.2.

```julia-repl
julia> a = [1, 2, 3];

julia> var"a"
3-element Array{Int64,1}:
 1
 2
 3

```

or create a new variable by assignment

```julia-repl
julia> var"msg" = "Hello, world!";

julia> msg
"Hello, world!"

```

---

<div class="post-metadata">

### Author: ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)
#### Post date: [July 24, 2020, 5:49am UTC](https://discourse.julialang.org/t/string-as-a-variable-name/2195/22 "2020-07-24T05:49:25Z")

</div>

> [@Shuhua](#):
>
> I cannot find the official documentation for this syntax

`?@var_str`, like all other [literal macros](https://docs.julialang.org/en/v1/manual/metaprogramming/#Non-Standard-String-Literals-1).

---

<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: [July 24, 2020, 1:22pm UTC](https://discourse.julialang.org/t/string-as-a-variable-name/2195/23 "2020-07-24T13:22:38Z")

</div>

It does not allow you to do anything new. It’s merely a different syntax to write out a variable name that looks like a string.

---

<div class="post-metadata">

### Author: ![johnmyleswhite](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/johnmyleswhite/32/31_2.png) [@johnmyleswhite](https://discourse.julialang.org/u/johnmyleswhite)
#### Post date: [July 24, 2020, 1:38pm UTC](https://discourse.julialang.org/t/string-as-a-variable-name/2195/24 "2020-07-24T13:38:09Z")

</div>

Doesn’t it let you create and refer to variables whose names are not valid identifiers, which is not normally possible without metaprogramming?

```julia
julia> 1 = 2
ERROR: syntax: invalid assignment location "1" around REPL[1]:1
Stacktrace:
 [1] top-level scope at REPL[1]:1

julia> var"1" = 2
2

```

---

<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: [July 24, 2020, 1:51pm UTC](https://discourse.julialang.org/t/string-as-a-variable-name/2195/25 "2020-07-24T13:51:07Z")

</div>

Errrr, well,

1. That’s still just “a different syntax”, and
2. It has nothing to do with what this thread is about…

---

<div class="post-metadata">

### Author: ![eckofoid](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/eckofoid/32/221638_2.png) [@eckofoid](https://discourse.julialang.org/u/eckofoid)
#### Post date: [September 18, 2020, 4:00am UTC](https://discourse.julialang.org/t/string-as-a-variable-name/2195/26 "2020-09-18T04:00:45Z")

</div>

I’m not convinced this should ever be done, but, it seems to do what you wanted:

```julia
julia> boo = ("name1", "name2", "name3")
("name1", "name2", "name3")

julia> parsley = (55.6, 33.9, 3692.3)
(55.6, 33.9, 3692.3)

julia> for i in 1:length(boo)
          eval(Meta.parse("var\"" * boo[i] * "\" = " * string(parsley[i])))
       end

julia> name1
55.6

julia> name2
33.9

julia> name3
3692.3

```

---

<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: [September 18, 2020, 4:52am UTC](https://discourse.julialang.org/t/string-as-a-variable-name/2195/27 "2020-09-18T04:52:32Z")

</div>

Never ever use parse to do metaprogramming.

---

<div class="post-metadata">

### Author: ![brett\_knoss](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/brett_knoss/32/13050_2.png) [@brett\_knoss](https://discourse.julialang.org/u/brett_knoss)
#### Post date: [May 30, 2021, 6:46pm UTC](https://discourse.julialang.org/t/string-as-a-variable-name/2195/28 "2021-05-30T18:46:02Z")

</div>

Is it possible to convert a variable name to a string or vice versa in a marco? I’m working with dataframes, but I might also try to do the same thing with dictionaries.

[Previous page](https://discourse.julialang.org/t/string-as-a-variable-name/2195.md?page=1)
