# Convert to string without losing the colon character for Symbol variables

**URL:** <https://discourse.julialang.org/t/convert-to-string-without-losing-the-colon-character-for-symbol-variables/100803>\
**Category:** General Usage\
**Tags:** question, strings\
**Created:** [June 25, 2023, 6:06am UTC](https://discourse.julialang.org/t/convert-to-string-without-losing-the-colon-character-for-symbol-variables/100803 "2023-06-25T06:06:53Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![David\_Sagan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/david_sagan/32/33792_2.png) [@David\_Sagan](https://discourse.julialang.org/u/David_Sagan)\
**Post date:** [June 25, 2023, 6:06am UTC](https://discourse.julialang.org/t/convert-to-string-without-losing-the-colon-character-for-symbol-variables/100803/1 "2023-06-25T06:06:53Z")

</div>

If I convert a Symbol to a string, the colon that is used to signify that it is a symbol is lost. For example,

```julia
julia> a = :abc
:abc

julia> string(a) # String does not have a colon
"abc"

```

I know I can create a very simple function to preserve the colon for Symbols but not alter the string formed for other variables but I was wondering if there is already an existing function for this.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 25, 2023, 8:20am UTC](https://discourse.julialang.org/t/convert-to-string-without-losing-the-colon-character-for-symbol-variables/100803/2 "2023-06-25T08:20:58Z")

</div>

The colon is not actually part of the `Symbol` object, which is why you don’t get the colon when you create a `String` from it. Similar to how `Vector` is not part of the vector object itself when printing an array.

You can do `':' * string(a)` to prepend the string with a colon.

---

<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:** [June 25, 2023, 11:18am UTC](https://discourse.julialang.org/t/convert-to-string-without-losing-the-colon-character-for-symbol-variables/100803/3 "2023-06-25T11:18:11Z")

</div>

> [@David\_Sagan](#):
>
> I was wondering if there is already an existing function for this.

There is [repr](https://docs.julialang.org/en/v1/base/strings/#Base.repr-Tuple%7BAny%7D):

```julia
julia> repr(:foo)
":foo"

```

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 25, 2023, 11:39am UTC](https://discourse.julialang.org/t/convert-to-string-without-losing-the-colon-character-for-symbol-variables/100803/4 "2023-06-25T11:39:43Z")

</div>

It isnot a string but show(a) looks like you want

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 25, 2023, 12:31pm UTC](https://discourse.julialang.org/t/convert-to-string-without-losing-the-colon-character-for-symbol-variables/100803/5 "2023-06-25T12:31:06Z")

</div>

> [@stevengj](#):
>
> There is [repr](https://docs.julialang.org/en/v1/base/strings/#Base.repr-Tuple%7BAny%7D):

That’s not guaranteed to give you the colon, because the colon is not part of the `Symbol`:

```julia
julia> Symbol("1")
Symbol("1")

julia> repr(Symbol("1"))
"Symbol(\"1\")"

```

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 25, 2023, 12:40pm UTC](https://discourse.julialang.org/t/convert-to-string-without-losing-the-colon-character-for-symbol-variables/100803/6 "2023-06-25T12:40:55Z")

</div>

To expand a bit on what I mean when I say “the colon is not part of the symbol”; contrary to popular belief, the prefixed colon is _not_ the “make this a symbol” operator:

```julia
julia> a = :1
1

julia> a isa Symbol
false

julia> typeof(a)
Int64

```

Used like this, the colon is a quasi-quote operator. This gets a bit technical, but this more or less means “give me the next fully-formed expression as the parser understands it”. For numbers, that means giving back a numeric literal, for some text that means a symbol and so on.

The issue is the following - `Symbol("foo bar")` is a perfectly valid symbol! You can’t produce it with `:foo bar` though, because the first fully formed expression after `:` is just `foo`:

```julia
julia> :foo bar
ERROR: ParseError:
# Error @ REPL[6]:1:5
:foo bar
# └──┘ ── extra tokens after end of expression
Stacktrace:
 [1] top-level scope
   @ none:1

```

(thanks to @c42f for the amazing PR on the most recent master branch! It’s very useful to explain things like this).

Similarly, `Symbol("1")` is a perfectly valid symbol, yet you can’t get it by writing `:1` because that returns an integer literal, as shown above.

So while I don’t know why you want the prefixed colon, note that it’s not part of the symbol; I’m not sure prefixing it in strings you create from a symbol is going to achieve what you’re ultimately trying to do. While `repr` doesn’t always give a leading `:` (and it can’t), it’s likely going to be what you’re looking for anyway.

---

<div class="post-metadata">

**Author:** ![David\_Sagan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/david_sagan/32/33792_2.png) [@David\_Sagan](https://discourse.julialang.org/u/David_Sagan)\
**Post date:** [June 25, 2023, 6:45pm UTC](https://discourse.julialang.org/t/convert-to-string-without-losing-the-colon-character-for-symbol-variables/100803/7 "2023-06-25T18:45:45Z")

</div>

I want to convert to a string so unfortunately show() does not work. repr() looks usable.

---

<div class="post-metadata">

**Author:** ![David\_Sagan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/david_sagan/32/33792_2.png) [@David\_Sagan](https://discourse.julialang.org/u/David_Sagan)\
**Post date:** [June 25, 2023, 7:02pm UTC](https://discourse.julialang.org/t/convert-to-string-without-losing-the-colon-character-for-symbol-variables/100803/8 "2023-06-25T19:02:34Z")

</div>

@Sukera: Thanks for the expanded reply. My usage is to print Dict key, value info where the key can be a Symbol like `:orientation` . I want the output to include the colon to alert the user that the key is a Symbol. There will not be any keys like `Symbol("1")` so indeed, for my purposes, `repr()` looks to be what I need.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 25, 2023, 8:32pm UTC](https://discourse.julialang.org/t/convert-to-string-without-losing-the-colon-character-for-symbol-variables/100803/9 "2023-06-25T20:32:36Z")

</div>

under the hood, repr calls show

```julia
julia> a=:foo
:foo

julia> sprint(show,a)
":foo"

julia> typeof(sprint(show,a))
String

```

---

<div class="post-metadata">

**Author:** ![frylock](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/frylock/32/50213_2.png) [@frylock](https://discourse.julialang.org/u/frylock)\
**Post date:** [June 26, 2023, 12:27pm UTC](https://discourse.julialang.org/t/convert-to-string-without-losing-the-colon-character-for-symbol-variables/100803/10 "2023-06-26T12:27:14Z")

</div>

@Sukera, did you mean [this PR](https://github.com/JuliaLang/julia/pull/44560)?

---

<div class="post-metadata">

**Author:** ![David\_Sagan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/david_sagan/32/33792_2.png) [@David\_Sagan](https://discourse.julialang.org/u/David_Sagan)\
**Post date:** [June 27, 2023, 6:10am UTC](https://discourse.julialang.org/t/convert-to-string-without-losing-the-colon-character-for-symbol-variables/100803/11 "2023-06-27T06:10:51Z")

</div>

@rocco_sprmnt21: Interesting! If I try to use string instead of sprint I get:

```julia
julia> string(show(a))
:foo"nothing"

```

I do not understand this output. Can you explain this? Thanks.

---

<div class="post-metadata">

**Author:** ![rocco\_sprmnt21](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rocco_sprmnt21/32/20127_2.png) [@rocco\_sprmnt21](https://discourse.julialang.org/u/rocco_sprmnt21)\
**Post date:** [June 27, 2023, 9:45am UTC](https://discourse.julialang.org/t/convert-to-string-without-losing-the-colon-character-for-symbol-variables/100803/12 "2023-06-27T09:45:58Z")

</div>

As far as I understand, the show(something) function sends to the stdout (the screen) ‘something’ but returns nothing.

```julia
julia> show(a)
:foo

```

Introducing this as an argument to the string function produces the output of the show() function, because the argument is executed, and transforms the returned value into a string, ie “nothing”.

---

<div class="post-metadata">

**Author:** ![Sukera](https://avatars.discourse-cdn.com/v4/letter/s/ce7236/32.png) [@Sukera](https://discourse.julialang.org/u/Sukera)\
**Post date:** [June 27, 2023, 9:49am UTC](https://discourse.julialang.org/t/convert-to-string-without-losing-the-colon-character-for-symbol-variables/100803/13 "2023-06-27T09:49:49Z")

</div>

> [@frylock](#):
>
> @Sukera, did you mean [this PR](https://github.com/JuliaLang/julia/pull/44560)?

No, I was referring to [this one](https://github.com/JuliaLang/julia/pull/46372), which produced the fancy error message in the syntax error, showing that `:foo` is already a fully formed expression.
