# Why does Julia think a vector is a string?

**URL:** <https://discourse.julialang.org/t/why-does-julia-think-a-vector-is-a-string/67078>\
**Category:** New to Julia\
**Created:** [August 27, 2021, 3:35am UTC](https://discourse.julialang.org/t/why-does-julia-think-a-vector-is-a-string/67078 "2021-08-27T03:35:00Z")\
**Posts on this page:** 13\
**Page:** 2

<div class="post-metadata">

**Author:** ![heliosdrm](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/heliosdrm/32/3851_2.png) [@heliosdrm](https://discourse.julialang.org/u/heliosdrm)\
**Post date:** [August 27, 2021, 8:47am UTC](https://discourse.julialang.org/t/why-does-julia-think-a-vector-is-a-string/67078/23 "2021-08-27T08:47:46Z")

</div>

> [@dgpdx](#):
>
> how can I get [1,2,3] from “[1,2,3]”?

You can also evaluate arbitrary expressions from strings in the global scope, with e.g.:

```julia
include_string(Main, "[1,2,3]")

```

or

```julia
eval(Meta.parse(["1,2,3"]))

```

But it is not the recommended way to go. For instance, this cannot be done locally inside a function. @mzaffalon’s proposal is better, although not applicable to arbitrary strings.

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 27, 2021, 8:51am UTC](https://discourse.julialang.org/t/why-does-julia-think-a-vector-is-a-string/67078/24 "2021-08-27T08:51:28Z")

</div>

That appears to work! I wish you’d seen the question when I first asked it! It would have saved me a week’s time!

It now works perfectly! How can I ever thank you enough!

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 27, 2021, 9:20am UTC](https://discourse.julialang.org/t/why-does-julia-think-a-vector-is-a-string/67078/25 "2021-08-27T09:20:40Z")

</div>

Your suggestion: `parse.(Int, split(strip(str, ['[', ']']), ',’))`  
works, but the resulting vector has spaces between each element, eg, [1, 2, 3].  
To save space, I’d prefer [1,2,3]. How do I modify the parse expression to avoid the spaces?

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [August 27, 2021, 10:06am UTC](https://discourse.julialang.org/t/why-does-julia-think-a-vector-is-a-string/67078/26 "2021-08-27T10:06:02Z")

</div>

What do you mean by “the vector has spaces”? Whether or not there are spaces is just a printing/display issue, the underlying vectors are the same irrespective of the whitespace between elements:

```julia
julia> [1, 2, 3] == [1,2,3] == [1 , 2 , 3]
true

```

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 27, 2021, 11:19am UTC](https://discourse.julialang.org/t/why-does-julia-think-a-vector-is-a-string/67078/27 "2021-08-27T11:19:53Z")

</div>

Here’s an example:

**julia\>** empty!(S)  
Stack{Any}(Deque [Any])

**julia\>** s = “[1,2,3]”  
“[1,2,3]”

**julia\>** push!(S,s)  
Stack{Any}(Deque [Any[“[1,2,3]”]])

**julia\>** t = parse.(Int, split(strip(s, [‘[’,‘]’]),‘,’))  
3-element Vector{Int64}:  
1  
2  
3

**julia\>** push!(S,t)  
Stack{Any}(Deque [Any[“[1,2,3]”, [1, 2, 3]]])

Notice the difference between pushing s and pushing t. I’d like to get rid of those spaces in t.

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [August 27, 2021, 11:30am UTC](https://discourse.julialang.org/t/why-does-julia-think-a-vector-is-a-string/67078/28 "2021-08-27T11:30:53Z")

</div>

There are no spaces in a vector. It’s just how Julia _prints_ vectors in some cases. There is no way to remove it, because it doesn’t exist.

(I mean, you can remove it from the printing, in principle, but it’s not really _there_ in the vector itself.)

---

<div class="post-metadata">

**Author:** ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)\
**Post date:** [August 27, 2021, 11:32am UTC](https://discourse.julialang.org/t/why-does-julia-think-a-vector-is-a-string/67078/29 "2021-08-27T11:32:04Z")

</div>

But again this is just a display issue, there are no “spaces” in the actual object `[1, 2, 3]`, which is just a `Vector{Int64}`, which by default gets displayed in the way you show when not displayed on its own:

```julia
julia> [[1, 2, 3]]
1-element Vector{Vector{Int64}}:
 [1, 2, 3]

```

I guess you could overwrite the `show` methods for this if it bothers you?

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 27, 2021, 11:33am UTC](https://discourse.julialang.org/t/why-does-julia-think-a-vector-is-a-string/67078/30 "2021-08-27T11:33:26Z")

</div>

OK, thanks for the explanation.

---

<div class="post-metadata">

**Author:** ![mzaffalon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mzaffalon/32/214168_2.png) [@mzaffalon](https://discourse.julialang.org/u/mzaffalon)\
**Post date:** [August 27, 2021, 11:33am UTC](https://discourse.julialang.org/t/why-does-julia-think-a-vector-is-a-string/67078/31 "2021-08-27T11:33:53Z")

</div>

> [@dgpdx](#):
>
> To save space, I’d prefer [1,2,3]. How do I modify the parse expression to avoid the spaces?

What **space** do you want to save?

Can you tell us what you are trying to achieve?

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 27, 2021, 11:34am UTC](https://discourse.julialang.org/t/why-does-julia-think-a-vector-is-a-string/67078/32 "2021-08-27T11:34:01Z")

</div>

How can I remove them from the printing?

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 27, 2021, 11:35am UTC](https://discourse.julialang.org/t/why-does-julia-think-a-vector-is-a-string/67078/33 "2021-08-27T11:35:27Z")

</div>

I’m trying to achieve a denser printout: [1,2,3] rather than [1, 2, 3].

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [August 27, 2021, 11:38am UTC](https://discourse.julialang.org/t/why-does-julia-think-a-vector-is-a-string/67078/34 "2021-08-27T11:38:55Z")

</div>

You’re confusing strings and arrays. You have an array with mixed elements:

> [@dgpdx](#):
>
> Any[“[1,2,3]”, [1, 2, 3]]

The first element is a printed representation of a string (`"[1,2,3]"`), the latter is a printed representation of an array (`[1, 2, 3]` or

```julia
[1, 2, 3]

```

doesn’t matter, as it’s _just how julia prints the array to the screen_).

* * *

Please enclose your code in triple backticks when posting to make it easier for people to read it and follow along. [This makes it easier for use to help you.](https://discourse.julialang.org/t/please-read-make-it-easier-to-help-you/14757)

---

<div class="post-metadata">

**Author:** ![dgpdx](https://avatars.discourse-cdn.com/v4/letter/d/45deac/32.png) [@dgpdx](https://discourse.julialang.org/u/dgpdx)\
**Post date:** [August 27, 2021, 11:39am UTC](https://discourse.julialang.org/t/why-does-julia-think-a-vector-is-a-string/67078/35 "2021-08-27T11:39:49Z")

</div>

OK, thanks for the explanation.

[Previous page](https://discourse.julialang.org/t/why-does-julia-think-a-vector-is-a-string/67078.md?page=1)
