# How do I avoid this error when using declared variables to create strings?

**URL:** <https://discourse.julialang.org/t/how-do-i-avoid-this-error-when-using-declared-variables-to-create-strings/9135>\
**Category:** New to Julia\
**Tags:** question\
**Created:** [February 17, 2018, 11:40pm UTC](https://discourse.julialang.org/t/how-do-i-avoid-this-error-when-using-declared-variables-to-create-strings/9135 "2018-02-17T23:40:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![krinsman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krinsman/32/3495_2.png) [@krinsman](https://discourse.julialang.org/u/krinsman)\
**Post date:** [February 17, 2018, 11:40pm UTC](https://discourse.julialang.org/t/how-do-i-avoid-this-error-when-using-declared-variables-to-create-strings/9135/1 "2018-02-17T23:40:43Z")

</div>

Here’s a MWE:

```julia
a = 3
b = 4

```

(This is from the second tutorial on [JuliaBox.com](http://JuliaBox.com).)

These commands work in isolation (i.e. in a cell by their own, these are IJulia notebooks in Jupyter):

```julia
"$(a+b)"

```

```julia
"""$a + $b"""

```

```julia
"""$(a+b)"""

```

```julia
"$a + $b"

```

However the following four commands (each in their own cell of the notebook):

```julia
"$a + $b"
"$(a+b)"

```

```julia
"$a + $b"
"""$(a+b)"""

```

```julia
"""$a + $b"""
"$(a+b)"

```

```julia
"""$a + $b"""
"""$(a+b)"""

```

all produce the same error as follows:

```julia
cannot document the following expression:

"$(a + b)"

Stacktrace:
 [1] error(::String, ::String, ::Vararg{String,N} where N) at ./error.jl:30
 [2] include_string(::String, ::String) at ./loading.jl:522

```

Similarly, the following four commands (each in their own cell of the notebook):

```julia
"$(a+b)"
"$a + $b"

```

```julia
"""$(a+b)"""
"$a + $b"

```

```julia
"$(a+b)"
"""$a + $b"""

```

```julia
"""$(a+b)"""
"""$a + $b"""

```

all produced the same following error:

```julia
cannot document the following expression:

"$(a) + $(b)"

Stacktrace:
 [1] error(::String, ::String, ::Vararg{String,N} where N) at ./error.jl:30
 [2] include_string(::String, ::String) at ./loading.jl:522

```

The output of `versioninfo()` is as follows:

```julia
Julia Version 0.6.2
Commit d386e40c17 (2017-12-13 18:08 UTC)
Platform Info:
  OS: Linux (x86_64-pc-linux-gnu)
  CPU: Intel(R) Xeon(R) CPU E5-2673 v3 @ 2.40GHz
  WORD_SIZE: 64
  BLAS: libopenblas (USE64BITINT DYNAMIC_ARCH NO_AFFINITY Haswell)
  LAPACK: libopenblas64_
  LIBM: libopenlibm
  LLVM: libLLVM-3.9.1 (ORCJIT, haswell)

```

Anyway at first I thought this might be a bug, but upon further reflection that seems more unlikely than me just doing something wrong (because this is literally the first time I have ever used the language).

So if someone could nudge me back into the right direction of how to use the language’s declared variable string features (the syntax is similar to BASH, which I like) I would greatly appreciate it. Thank you for your time and please enjoy your weekend!

---

<div class="post-metadata">

**Author:** ![JackDevine](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jackdevine/32/1048_2.png) [@JackDevine](https://discourse.julialang.org/u/JackDevine)\
**Post date:** [February 18, 2018, 1:00am UTC](https://discourse.julialang.org/t/how-do-i-avoid-this-error-when-using-declared-variables-to-create-strings/9135/2 "2018-02-18T01:00:39Z")

</div>

julia thinks that you are trying to document the lower expression.

The way to get around your problem is to assign your multiline string to a variable, e.g.

```julia
x = """$(a) + $(b)"""
"""$(a+b)"""

```

then julia will no longer be confused.

Just so that you know, the syntax

```julia
"""mulitlinestring"""
julia_expression

```

is reserved for documenting expressions, which is really useful of you write a lot of functions. Here is a rough example:

```julia
"""
    foo(x)

Multiply `x` by two and add three.
"""
function foo(x)
    2x + 3
end

```

Then later on if you want to know what `foo` does you can ask for documentation by pressing `?` at the REPL (works in a notebook too).

```julia
julia> foo(2)
7

help?> foo
search: floor pointer_from_objref OverflowError RoundFromZero FileMonitor

  foo(x)

  Multiply x by two and add three.

```

---

<div class="post-metadata">

**Author:** ![krinsman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krinsman/32/3495_2.png) [@krinsman](https://discourse.julialang.org/u/krinsman)\
**Post date:** [February 20, 2018, 12:25am UTC](https://discourse.julialang.org/t/how-do-i-avoid-this-error-when-using-declared-variables-to-create-strings/9135/3 "2018-02-20T00:25:08Z")

</div>

Dear Jack,

This is really helpful, thank you a lot for this. I really appreciate it.

To clarify, the triple quotes in Julia serve the same purpose as in Python (docstrings)?

How does this explain the problems which occur only using the single quotes?

```julia
"$(a+b)"
"$a + $b"

```

and

```julia
"$a + $b"
"$(a+b)"

```

also didn’t work, and they have no docstring quotes.

Is this an issue with new lines or white space? I.e. is Julia like C or C++ where every line needs to be ended with a semi-colon?

---

<div class="post-metadata">

**Author:** ![swt30](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/swt30/32/4667_2.png) [@swt30](https://discourse.julialang.org/u/swt30)\
**Post date:** [February 20, 2018, 10:48am UTC](https://discourse.julialang.org/t/how-do-i-avoid-this-error-when-using-declared-variables-to-create-strings/9135/4 "2018-02-20T10:48:56Z")

</div>

There’s no such thing as a “docstring quote”—the triple-quotes are just another way of creating a string that lets you do some useful things like put newlines and other quotes inside. Any string can be a docstring if you put it directly before another expression. This means that

```julia
"$(a+b)"
"$a + $b"

```

and

```julia
"""$(a+b)"""
"""$a + $b"""

```

are both saying the same thing: apply the docstring `"$(a+b)"` to the string `"$a + $b"`—but you can’t document a string, hence the error.

---

<div class="post-metadata">

**Author:** ![krinsman](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/krinsman/32/3495_2.png) [@krinsman](https://discourse.julialang.org/u/krinsman)\
**Post date:** [February 20, 2018, 8:49pm UTC](https://discourse.julialang.org/t/how-do-i-avoid-this-error-when-using-declared-variables-to-create-strings/9135/5 "2018-02-20T20:49:50Z")

</div>

This clarifies a lot of misunderstandings I had, thank you again.

**EDIT:** I think I have figured it out – the problem is essentially that I am/was lazy and didn’t bother to wrap the expressions with a call to `println()`, so the expressions/statements weren’t separated from each other. I.e. I was using it “too interactively”. Here are the relevant parts of the webpage [https://docs.julialang.org/en/release-0.4/manual/noteworthy-differences/](https://docs.julialang.org/en/release-0.4/manual/noteworthy-differences/) which seem to indicate this to me, although I still need to go back to the tutorial later and see whether this works:

> Julia has no line continuation syntax: if, at the end of a line, the input so far is a complete expression, it is considered done; otherwise the input continues. One way to force an expression to continue is to wrap it in parentheses.

> In Julia, whitespace is significant, unlike C/C++, so care must be taken when adding/removing whitespace from a Julia program.

> Julia does not require the use of semicolons to end statements. The results of expressions are not automatically printed (except at the interactive prompt, i.e. the REPL), and lines of code do not need to end with semicolons. println() or @printf() can be used to print specific output. In the REPL, ; can be used to suppress output. ; also has a different meaning within , something to watch out for. ; can be used to separate expressions on a single line, but are not strictly necessary in many cases, and are more an aid to readability.

**/EDIT**

_original post for historical reasons I suppose? it’s not really that important_

So just to be clear, these statements are considered “directly before” each other because there is no end of statement character like a semi-colon or something similar?

Here [https://docs.julialang.org/en/release-0.4/manual/noteworthy-differences/](https://docs.julialang.org/en/release-0.4/manual/noteworthy-differences/) it says:

> Julia discourages the used of semicolons to end statements. The results of statements are not automatically printed (except at the interactive prompt), and lines of code do not need to end with semicolons.

This is confusing because if a semicolon is not needed to end the statement, but a line break is not enough, then what is sufficient to end the statement?

I guess I am somewhat surprised that docstrings can be placed anywhere, not just inside of a function definition. And it is still somewhat unclear to me what I could have done instead to make the code work.
