# 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:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![NickNack](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nicknack/32/1792_2.png) [@NickNack](https://discourse.julialang.org/u/NickNack)\
**Post date:** [February 28, 2017, 12:34pm UTC](https://discourse.julialang.org/t/string-as-a-variable-name/2195/7 "2017-02-28T12:34:12Z")

</div>

Doing it as a macro seems to work correctly inside a function (i.e. variables are created only in the local scope):

```julia
macro string_as_varname_macro(s::AbstractString, v::Any)
	s = Symbol(s)
	esc(:($s = $v))
end

function string_as_varname_function(s::AbstractString, v::Any)
	s = Symbol(s)
	@eval (($s) = ($v))
end

function test()
	string_as_varname_function("a",2)
	@string_as_varname_macro("b",3)
	println("(a,b) = ($a,$b)")
end

test()
isdefined(:a) && println("a = $a")
isdefined(:b) && println("b = $b")
nothing

```

Output:

```julia
(a,b) = (2,3)
a = 2

```

I’ve only been working with Julia for a few weeks, so please explain if this has some unintended side effect.

---

_[View the full topic](https://discourse.julialang.org/t/string-as-a-variable-name/2195)._
