# Print command in Julia

**URL:** https://discourse.julialang.org/t/print-command-in-julia/121213
**Category:** General Usage
**Created:** [October 12, 2024, 6:49am UTC](https://discourse.julialang.org/t/print-command-in-julia/121213 "2024-10-12T06:49:24Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)
#### Post date: [October 12, 2024, 6:49am UTC](https://discourse.julialang.org/t/print-command-in-julia/121213/1 "2024-10-12T06:49:25Z")

</div>

Hello,  
Should there be a `" "` sign in Julia to print variables and strings? In C/C++ language, `" "` sign is not needed to print the contents of a variable.

Thank you.

---

<div class="post-metadata">

### Author: ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)
#### Post date: [October 12, 2024, 7:33am UTC](https://discourse.julialang.org/t/print-command-in-julia/121213/2 "2024-10-12T07:33:07Z")

</div>

Can you be a bit more precise? I am not sure why a space (if that is what you refer to with `" "`) should be necessary.

If you do

```julia
a=3
print(a)

```

it just prints (the value of) a. If you also want a new line starting after that, use `println(a)`.

---

<div class="post-metadata">

### Author: ![goerz](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/goerz/32/3269_2.png) [@goerz](https://discourse.julialang.org/u/goerz)
#### Post date: [October 12, 2024, 9:38am UTC](https://discourse.julialang.org/t/print-command-in-julia/121213/3 "2024-10-12T09:38:38Z")

</div>

Your message is quite unclear. Are you looking for `”$(var1)moretext”`? That is, you can use parentheses if there is no space after the variable to be printed (or if you need to print something more complex: inside the parentheses, you can put arbitrary code)

---

<div class="post-metadata">

### Author: ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)
#### Post date: [October 12, 2024, 9:38am UTC](https://discourse.julialang.org/t/print-command-in-julia/121213/4 "2024-10-12T09:38:46Z")

</div>

Hello,  
Thank you so much for your reply.  
Why didn’t you use `$a`? Is it because your print statement doesn’t use the `" "` sign?

> To print a variable we need to use a $ before the variable name.

---

<div class="post-metadata">

### Author: ![jling](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jling/32/212909_2.png) [@jling](https://discourse.julialang.org/u/jling)
#### Post date: [October 12, 2024, 9:45am UTC](https://discourse.julialang.org/t/print-command-in-julia/121213/5 "2024-10-12T09:45:39Z")

</div>

> [@hack3rcon](#):
>
> To print a variable we need to use a $ before the variable name.

who said this? where? can you provide more context. That is true when you want to interpolate a variable into a string

---

<div class="post-metadata">

### Author: ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)
#### Post date: [October 12, 2024, 9:55am UTC](https://discourse.julialang.org/t/print-command-in-julia/121213/6 "2024-10-12T09:55:41Z")

</div>

Well, you are mixing maybe two things. Printing and generating strings.

If you just want to print something `print(a)` would dispatch on a method that prints the value of the variable `a` but if you want to generate a string containing (the value of) a, then you have to [interpolate](https://docs.julialang.org/en/v1/manual/strings/#string-interpolation) it, and that is what the `$a` does within a string.

So you could do `s="$a"` and get a string and sure, you can also just call the print function of that string `print("$a")` afterwards (even without storing that in `s`).  
That is possible but not necessary to print `a`. And there might be cases where both are different, since `"$a"` internally calls `string(a)` (and not `print(a)`).

_edit:_ So I am not sure where your quote is from, but it is not correct, you can but you do not have to.

---

<div class="post-metadata">

### Author: ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)
#### Post date: [October 12, 2024, 4:08pm UTC](https://discourse.julialang.org/t/print-command-in-julia/121213/7 "2024-10-12T16:08:08Z")

</div>

Hello,  
Please take a look at [https://www.amazon.com/Julia-Programming-Beginners-Undergraduate-Computer/dp/303073935X](https://www.amazon.com/Julia-Programming-Beginners-Undergraduate-Computer/dp/303073935X).

---

<div class="post-metadata">

### Author: ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)
#### Post date: [October 12, 2024, 4:13pm UTC](https://discourse.julialang.org/t/print-command-in-julia/121213/8 "2024-10-12T16:13:06Z")

</div>

Hello,  
Thank you so much for your reply.  
So, when our variable is of string type, we use `$` before the variable name.

---

<div class="post-metadata">

### Author: ![kellertuer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kellertuer/32/220707_2.png) [@kellertuer](https://discourse.julialang.org/u/kellertuer)
#### Post date: [October 12, 2024, 4:21pm UTC](https://discourse.julialang.org/t/print-command-in-julia/121213/9 "2024-10-12T16:21:06Z")

</div>

Hm, then you misread what I wrote or at least are not precise  
If you have a string like

```julia
s = "HI!"
print(s)

```

works as well just fine

But if you want to bring a variable _into_ a string – the so-called interpolation, _then_ you do a `$` upfront. That is maybe comparable, but not the same, as the `%s` fields in C/C++s `sprint`s

An example

```julia
a=1 # Int
b=0.987 # Float
c = 1//2 # Rational
s = "We have $a, $b, and $c"

```

Then the String `s` looks like `"We have 1, 0.987, and 1//2"`  
So the `$` is used `$` when _constructing_ a string. Sure this might be a string in the argument of a print

---

<div class="post-metadata">

### Author: ![hack3rcon](https://avatars.discourse-cdn.com/v4/letter/h/96bed5/32.png) [@hack3rcon](https://discourse.julialang.org/u/hack3rcon)
#### Post date: [October 13, 2024, 5:50am UTC](https://discourse.julialang.org/t/print-command-in-julia/121213/10 "2024-10-13T05:50:38Z")

</div>

Hi,  
Thank you so much.
