# How do I initialize an empty array / vector of strings, and then push strings into

**URL:** <https://discourse.julialang.org/t/how-do-i-initialize-an-empty-array-vector-of-strings-and-then-push-strings-into/56980>\
**Category:** General Usage\
**Created:** [March 11, 2021, 10:39pm UTC](https://discourse.julialang.org/t/how-do-i-initialize-an-empty-array-vector-of-strings-and-then-push-strings-into/56980 "2021-03-11T22:39:56Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![BridgeBot](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bridgebot/32/21491_2.png) [@BridgeBot](https://discourse.julialang.org/u/BridgeBot)\
**Post date:** [March 11, 2021, 10:39pm UTC](https://discourse.julialang.org/t/how-do-i-initialize-an-empty-array-vector-of-strings-and-then-push-strings-into/56980/1 "2021-03-11T22:39:57Z")

</div>

How do I initialize an empty array / vector of strings, and then push strings into it? When I do `t = Vector{String}` , I get this error when I then push into it:

```julia
push!(t, "a")
ERROR: MethodError: no method matching push!(::Type{Array{String,1}}, ::String)
Closest candidates are:
  push!(::Any, ::Any, ::Any) at abstractarray.jl:2252
  push!(::Any, ::Any, ::Any, ::Any...) at abstractarray.jl:2253
  push!(::Array{Any,1}, ::Any) at array.jl:940

```

and `t = Array{String}` results in the same error.

(I want to specify the type that the vector will be filled with in advance because the array will subsequently go into a namedtuple)

Note that the original poster on Slack cannot see your response here on Discourse. Consider _transcribing the appropriate answer back to Slack_, or pinging the poster here on Discourse so they can _follow this thread_.  
[(Original message :slack:)](https://julialang.slack.com/archives/C6A044SQH/p1615498783394900?thread_ts=1615498783.394900&cid=C6A044SQH) [(More Info)](https://github.com/JuliaCommunity/SlackBridge)

---

<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:** [March 11, 2021, 11:32pm UTC](https://discourse.julialang.org/t/how-do-i-initialize-an-empty-array-vector-of-strings-and-then-push-strings-into/56980/2 "2021-03-11T23:32:43Z")

</div>

```julia
julia> t = String[]
String[]

julia> push!(t,"a string")
1-element Array{String,1}:
 "a string"

julia> t
1-element Array{String,1}:
 "a string"

```

Another way

```julia
julia> w = Vector{String}()
String[]

julia> push!(w,"another string")
1-element Array{String,1}:
 "another string"

julia> w
1-element Array{String,1}:
 "another string"

```

---

<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:** [January 12, 2023, 2:34pm UTC](https://discourse.julialang.org/t/how-do-i-initialize-an-empty-array-vector-of-strings-and-then-push-strings-into/56980/3 "2023-01-12T14:34:04Z")

</div>

2 posts were split to a new topic: [Understanding push!](https://discourse.julialang.org/t/understanding-push/92872)
