# Attempting to print a string with an array

**URL:** <https://discourse.julialang.org/t/attempting-to-print-a-string-with-an-array/58614>\
**Category:** New to Julia\
**Created:** [April 5, 2021, 1:40pm UTC](https://discourse.julialang.org/t/attempting-to-print-a-string-with-an-array/58614 "2021-04-05T13:40:11Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![EMCP](https://avatars.discourse-cdn.com/v4/letter/e/db5fbb/32.png) [@EMCP](https://discourse.julialang.org/u/EMCP)\
**Post date:** [April 5, 2021, 1:40pm UTC](https://discourse.julialang.org/t/attempting-to-print-a-string-with-an-array/58614/1 "2021-04-05T13:40:11Z")

</div>

please forgive this very newb question. I am attempting to run through the book “Neural Networks from Scratch with Python” … but with JuliaLang instead

I want to do the following but it’s perhaps my bias towards python getting me to print in a maybe wrong way… how do I concatenate a string with an array to print its contents nicely?

Thanks

```julia
## Ch02 - Coding our first Neurons

print("Starting Exercise")

inputs = [1, 2, 3]
weights = [0.2, 0.3, -0.5]

bias = 2

print("inputs: " + string.(inputs))
print("weights: " + string.(weights))
print("bias: " + string.(bias))

```

```julia
% julia ch02_coding-first-neurons/ex01.jl
Starting ExerciseERROR: LoadError: MethodError: no method matching +(::String, ::Vector{String})
Closest candidates are:
  +(::Any, ::Any, ::Any, ::Any...) at operators.jl:560
  +(::Array, ::Array...) at arraymath.jl:43
  +(::SparseArrays.AbstractSparseMatrixCSC, ::Array) at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/SparseArrays/src/sparsematrix.jl:1744
  ...
Stacktrace:
 [1] top-level scope

```

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [April 5, 2021, 1:44pm UTC](https://discourse.julialang.org/t/attempting-to-print-a-string-with-an-array/58614/2 "2021-04-05T13:44:52Z")

</div>

Replace `+` with `*` and everything will work.

---

<div class="post-metadata">

**Author:** ![EMCP](https://avatars.discourse-cdn.com/v4/letter/e/db5fbb/32.png) [@EMCP](https://discourse.julialang.org/u/EMCP)\
**Post date:** [April 5, 2021, 2:15pm UTC](https://discourse.julialang.org/t/attempting-to-print-a-string-with-an-array/58614/3 "2021-04-05T14:15:26Z")

</div>

seems to yield a different error

```julia

print("inputs: "*string.(inputs))  

```

```julia
% julia ch02_coding-first-neurons/ex01.jl
Starting Exercise
ERROR: LoadError: MethodError: no method matching *(::String, ::Vector{String})
Closest candidates are:
  *(::Any, ::Any, ::Any, ::Any...) at operators.jl:560
  *(::Union{AbstractChar, AbstractString}, ::Union{AbstractChar, AbstractString}...) at strings/basic.jl:260
  *(::Union{SparseArrays.AbstractSparseMatrixCSC{TA, Ti}, SubArray{TA, 2, var"#s832", Tuple{Base.Slice{Base.OneTo{Int64}}, I}, L} where {I<:AbstractUnitRange, var"#s832"<:SparseArrays.AbstractSparseMatrixCSC{TA, Ti}, L}} where Ti, ::Union{StridedVector{T} where T, BitVector}) where TA at /Users/julia/buildbot/worker/package_macos64/build/usr/share/julia/stdlib/v1.6/SparseArrays/src/linalg.jl:50
  ...
Stacktrace:
 [1] top-level scope
   @ ~/nnfs-julialang/ch02_coding-first-neurons/ex01.jl:10
in expression starting at /nnfs-julialang/ch02_coding-first-neurons/ex01.jl:10

```

---

<div class="post-metadata">

**Author:** ![Jeff\_Emanuel](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jeff_emanuel/32/15440_2.png) [@Jeff\_Emanuel](https://discourse.julialang.org/u/Jeff_Emanuel)\
**Post date:** [April 5, 2021, 2:27pm UTC](https://discourse.julialang.org/t/attempting-to-print-a-string-with-an-array/58614/4 "2021-04-05T14:27:40Z")

</div>

> [@EMCP](#):
>
> I am attempting to run through the book “Neural Networks from Scratch with Python” … but with JuliaLang instead

You may be interested in this page: [How To Build An Artificial Neural Network From Scratch In Julia | by Bernard Brenyah | Towards Data Science](https://towardsdatascience.com/how-to-build-an-artificial-neural-network-from-scratch-in-julia-c839219b3ef8)

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 5, 2021, 2:39pm UTC](https://discourse.julialang.org/t/attempting-to-print-a-string-with-an-array/58614/5 "2021-04-05T14:39:53Z")

</div>

> [@EMCP](#):
>
> how do I concatenate a string with an array to print its contents nicely?

The easiest way to do this is to skip calling `string` at all:

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

julia> println("input: ", inputs)
input: [1, 2, 3]

```

The `println` function accepts multiple arguments, and it already knows how to print arrays.

Even easier, you can use the `@show` macro:

```julia
julia> @show inputs
inputs = [1, 2, 3]

```

---

<div class="post-metadata">

**Author:** ![EMCP](https://avatars.discourse-cdn.com/v4/letter/e/db5fbb/32.png) [@EMCP](https://discourse.julialang.org/u/EMCP)\
**Post date:** [April 5, 2021, 2:40pm UTC](https://discourse.julialang.org/t/attempting-to-print-a-string-with-an-array/58614/6 "2021-04-05T14:40:30Z")

</div>

I figured it out with the following change

```julia
## Ch02 - Coding our first Neurons

println("Starting Exercise")

inputs = [1, 2, 3]
weights = [0.2, 0.3, -0.5]

bias = 2

print("inputs: ",string.(inputs))

```

Ouput:

```julia
% julia ch02_coding-first-neurons/ex01.jl
Starting Exercise
inputs: ["1", "2", "3"]%   

```

I guess my final question is… am I experiencing different answers based on different versions of Julia ?

I saw some references to

```julia
@printf

```

[https://docs.julialang.org/en/v1/stdlib/Printf/](https://docs.julialang.org/en/v1/stdlib/Printf/)

this suggested `*` didn’t work either, as mentioned earlier in my last post…

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 5, 2021, 2:46pm UTC](https://discourse.julialang.org/t/attempting-to-print-a-string-with-an-array/58614/7 "2021-04-05T14:46:16Z")

</div>

> [@EMCP](#):
>
> am I experiencing different answers based on different versions of Julia ?

I don’t think so–you can use `*` to concatenate two strings in any Julia version. What you can’t do is concatenate a string to a _vector_ of strings, which is what `string.(inputs)` produces:

```julia
julia> "hello" * "world"
"helloworld"

julia> "hello" * ["world", "world"]
ERROR: MethodError: no method matching *(::String, ::Vector{String})

```

---

<div class="post-metadata">

**Author:** ![rdeits](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rdeits/32/286_2.png) [@rdeits](https://discourse.julialang.org/u/rdeits)\
**Post date:** [April 5, 2021, 2:47pm UTC](https://discourse.julialang.org/t/attempting-to-print-a-string-with-an-array/58614/8 "2021-04-05T14:47:13Z")

</div>

> [@EMCP](#):
>
> I saw some references to
> 
> ```julia
> @printf
> 
> ```

You can do that too. `Printf` is a _package_ in the standard library, so you need to load it with `using` or `import` first:

```julia
julia> using Printf

julia> @printf "inputs: %s" inputs
inputs: [1, 2, 3]

```
