# How to create a vector of string elements from a combination of different strings and numbers?

**URL:** <https://discourse.julialang.org/t/how-to-create-a-vector-of-string-elements-from-a-combination-of-different-strings-and-numbers/98400>\
**Category:** General Usage\
**Tags:** strings\
**Created:** [May 5, 2023, 9:16pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-string-elements-from-a-combination-of-different-strings-and-numbers/98400 "2023-05-05T21:16:07Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)\
**Post date:** [May 5, 2023, 9:16pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-string-elements-from-a-combination-of-different-strings-and-numbers/98400/1 "2023-05-05T21:16:07Z")

</div>

I know how to combine a string `"s"` with a vector of integers `i` to get a string `"si"`. For example, if I want to create the vector `["a1","a2","a3"]`, I do

```julia
 "a".* string.([1,2,3])
#3-element Vector{String}:
# "a1"
# "a2"
# "a3"

```

**How can I create a vector using this same “procedure” but now with than one string “s”. For instance, if I want to create a vector `["a1","b1","a2","b2","a3","b3"]`?**

I tried the line above, but it failed.

```julia
["a","b"].*string.([1,2,3])

```

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [May 5, 2023, 9:23pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-string-elements-from-a-combination-of-different-strings-and-numbers/98400/2 "2023-05-05T21:23:31Z")

</div>

You need to broadcast between different dimensions (to solve it with broadcasting).

```julia
julia> vec(["a","b"].*string.([1,2,3]'))
6-element Vector{String}:
 "a1"
 "b1"
 "a2"
 "b2"
 "a3"
 "b3"

```

---

<div class="post-metadata">

**Author:** ![Hugo](https://avatars.discourse-cdn.com/v4/letter/h/b38774/32.png) [@Hugo](https://discourse.julialang.org/u/Hugo)\
**Post date:** [May 5, 2023, 9:28pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-string-elements-from-a-combination-of-different-strings-and-numbers/98400/3 "2023-05-05T21:28:33Z")

</div>

Thank you so much, @GunnarFarneback.

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [May 5, 2023, 11:51pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-string-elements-from-a-combination-of-different-strings-and-numbers/98400/4 "2023-05-05T23:51:49Z")

</div>

I don’t know if this is helpful but as noted in [An exercise in DataFrames.jl transformation minilanguage | Blog by Bogumił Kamiński](https://bkamins.github.io/julialang/2022/05/06/minilanguage.html) you can also remove the `,` in the number array.

```julia
vec(string.(["a", "b"], [1 2 3]))
6-element Vector{String}:
 "a1"
 "b1"
 "a2"
 "b2"
 "a3"
 "b3"

```

or without the `vec` you get a matrix

```julia
julia> string.(["x", "y"], [1 2 3])
2×3 Matrix{String}:
 "x1" "x2" "x3"
 "y1" "y2" "y3"

```

@GunnarFarneback if you have a moment could you explain what the `'` operator is and how it works here?

> [@GunnarFarneback](#):
>
> ` vec(["a","b"].*string.([1,2,3]'))`

I tried looking it up in the docs but only found the ‘adjoint’ function for retiring complex conjugates?

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [May 6, 2023, 7:42am UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-string-elements-from-a-combination-of-different-strings-and-numbers/98400/5 "2023-05-06T07:42:00Z")

</div>

Here it’s just used as a shortcut to transform to a row vector.

```julia
julia> size([1,2,3]')
(1, 3)

```

The more general solution is to use `permutedims`, or for literals to use literal syntax to construct a vector along the desired dimension:

```julia
julia> size([1, 2, 3])
(3,)

julia> size([1 2 3])
(1, 3)

julia> size([1; 2; 3])
(3,)

julia> size([1;; 2;; 3])
(1, 3)

julia> size([1;;; 2;;; 3])
(1, 1, 3)

julia> size([1;;;; 2;;;; 3])
(1, 1, 1, 3)

```

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [May 6, 2023, 10:34pm UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-string-elements-from-a-combination-of-different-strings-and-numbers/98400/6 "2023-05-06T22:34:35Z")

</div>

Got it thanks so much! Just wondering since these transformations yield different types

```julia
julia> typeof([1,2,3]')
LinearAlgebra.Adjoint{Int64, Vector{Int64}}

```

whereas

```julia
julia> typeof([1 2 3])
Matrix{Int64} (alias for Array{Int64, 2})

```

Are there use cases where the resultant type affects performance and we should choose one over the other? Or is that purely a function of whether the elements in the array itself are type stable?

Sorry if this is a silly question but just trying to get a better grasp of the fundamentals here.

---

<div class="post-metadata">

**Author:** ![GunnarFarneback](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/gunnarfarneback/32/1827_2.png) [@GunnarFarneback](https://discourse.julialang.org/u/GunnarFarneback)\
**Post date:** [May 7, 2023, 9:47am UTC](https://discourse.julialang.org/t/how-to-create-a-vector-of-string-elements-from-a-combination-of-different-strings-and-numbers/98400/7 "2023-05-07T09:47:49Z")

</div>

It’s a bit of a misuse (but a convenient one) to employ `'` outside of a Linear Algebra context. The `LinearAlgebra.Adjoint` type is an example of a wrapper type which keeps the original array in the background but acts like some operation was applied. This is used with multiple dispatch for optimizations. E.g. `x''` returns `x` itself without doing any work at all and `x' * x` is dispatched to a method which calls the `dot` function.
