# How to use string substitution in variable names

**URL:** https://discourse.julialang.org/t/how-to-use-string-substitution-in-variable-names/55192
**Category:** New to Julia
**Tags:** strings
**Created:** [February 13, 2021, 5:57am UTC](https://discourse.julialang.org/t/how-to-use-string-substitution-in-variable-names/55192 "2021-02-13T05:57:30Z")
**Posts on this page:** 1
**Showing post:** 5

<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: [February 13, 2021, 4:08pm UTC](https://discourse.julialang.org/t/how-to-use-string-substitution-in-variable-names/55192/5 "2021-02-13T16:08:58Z")

</div>

> [@samerb](#):
>
> I would like to do something like the following but that doesn’t work. How can I do this? Maybe use `$` somehow? Edit: To be clear, I am trying to create the first 7 unit vectors. Thanks

Don’t use metaprogramming or generated variable names. Use a data structure. For example, an array `e` of the 7 unit vectors in ℝ⁷ can be constructed with:

```julia
e = [let v = zeros(7); v[i] = 1; v; end for i in 1:7 ]

```

This way, you can refer to `e[i]` programmatically rather than (as you seem to want) having 7 separate variables `e_1`, `e_2`, …, `e_7`. For example:

```julia
julia> e[3]
7-element Array{Float64,1}:
 0.0
 0.0
 1.0
 0.0
 0.0
 0.0
 0.0

```

See also this thread: [How to warn new users away from metaprogramming](https://discourse.julialang.org/t/how-to-warn-new-users-away-from-metaprogramming/35022)

---

_[View the full topic](https://discourse.julialang.org/t/how-to-use-string-substitution-in-variable-names/55192)._
