# String conversion from Symbol with Unicode does not yield a string, which is intended to be the same

**URL:** https://discourse.julialang.org/t/string-conversion-from-symbol-with-unicode-does-not-yield-a-string-which-is-intended-to-be-the-same/51289
**Category:** New to Julia
**Tags:** question, bug
**Created:** [December 5, 2020, 8:22am UTC](https://discourse.julialang.org/t/string-conversion-from-symbol-with-unicode-does-not-yield-a-string-which-is-intended-to-be-the-same/51289 "2020-12-05T08:22:07Z")
**Posts on this page:** 7
**Page:** 1

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [December 5, 2020, 8:22am UTC](https://discourse.julialang.org/t/string-conversion-from-symbol-with-unicode-does-not-yield-a-string-which-is-intended-to-be-the-same/51289/1 "2020-12-05T08:22:07Z")

</div>

Hi, I’m often use a conversion from Symbol to String. For example,  
`String(:a) == "a" # true`

However, when adding `\dot`, it yields a string which is different from intended string. For example,

`String(:a\dot) == "a\dot" # with tab completion, false`

I have no idea how to result in the intended result. Why are they different?

---

<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: [December 5, 2020, 9:06am UTC](https://discourse.julialang.org/t/string-conversion-from-symbol-with-unicode-does-not-yield-a-string-which-is-intended-to-be-the-same/51289/2 "2020-12-05T09:06:15Z")

</div>

The answer lies in unicode:

```julia
julia> "ȧ" |> collect
2-element Array{Char,1}:
 'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
 '': Unicode U+0307 (category Mn: Mark, nonspacing)

julia> String(:ȧ) |> collect
1-element Array{Char,1}:
 'ȧ': Unicode U+0227 (category Ll: Letter, lowercase)

julia> String(Symbol("ȧ")) |> collect
2-element Array{Char,1}:
 'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
 '': Unicode U+0307 (category Mn: Mark, nonspacing)

```

These two seemingly equivalent things are represented differently, though I cannot tell you why these entered like this differ. Presumably there’s some magic in the tab completion that simplifies/normalizes the representation to make it smaller?

---

<div class="post-metadata">

### Author: ![iHany](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ihany/32/18151_2.png) [@iHany](https://discourse.julialang.org/u/iHany)
#### Post date: [December 5, 2020, 9:09am UTC](https://discourse.julialang.org/t/string-conversion-from-symbol-with-unicode-does-not-yield-a-string-which-is-intended-to-be-the-same/51289/3 "2020-12-05T09:09:49Z")

</div>

Oh… I see. But it’s quite undesirable for my stuffs.  
Do you have any idea to avoid this undesirable situation? 🙂

---

<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: [December 5, 2020, 9:18am UTC](https://discourse.julialang.org/t/string-conversion-from-symbol-with-unicode-does-not-yield-a-string-which-is-intended-to-be-the-same/51289/4 "2020-12-05T09:18:20Z")

</div>

Avoid hardcoding symbols via `:a` and instead do `Symbol("a")` - that stays consistent, as you can see from the third test.

I’m not sure if these two are supposed to be equivalent, so it’s also a good idea to open an issue about this 🙂

---

<div class="post-metadata">

### Author: ![sijo](https://avatars.discourse-cdn.com/v4/letter/s/da6949/32.png) [@sijo](https://discourse.julialang.org/u/sijo)
#### Post date: [December 5, 2020, 1:05pm UTC](https://discourse.julialang.org/t/string-conversion-from-symbol-with-unicode-does-not-yield-a-string-which-is-intended-to-be-the-same/51289/5 "2020-12-05T13:05:33Z")

</div>

Identifiers in Julia are NFC-normalized. From the [documentation](https://docs.julialang.org/en/v1/manual/variables/#Allowed-Variable-Names):

> Some Unicode characters are considered to be equivalent in identifiers. Different ways of entering Unicode combining characters (e.g., accents) are treated as equivalent (specifically, Julia identifiers are [NFC](http://www.macchiato.com/unicode/nfc-faq)-normalized). The Unicode characters `ɛ` (U+025B: Latin small letter open e) and `µ` (U+00B5: micro sign) are treated as equivalent to the corresponding Greek letters, because the former are easily accessible via some input methods.

See also this issue: [canonicalize unicode identifiers · Issue #5434 · JuliaLang/julia · GitHub](https://github.com/JuliaLang/julia/issues/5434)

I don’t know if the different behaviors of `:...` and `Symbol(...)` is intended. I think `Symbol` is meant to allow creating symbols for invalid identifiers, see for example [this part](https://docs.julialang.org/en/v1/base/base/#var%22name%22) of the documentation:

> The syntax `var"#example#"` refers to a variable named `Symbol("#example#")` , even though `#example#` is not a valid Julia identifier name.

It seems reasonable that symbols declared with `:` would be normalized but maybe it’s worth filing an issue to clarify/document this potential gotcha?

---

<div class="post-metadata">

### Author: ![nilshg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/nilshg/32/2283_2.png) [@nilshg](https://discourse.julialang.org/u/nilshg)
#### Post date: [December 5, 2020, 2:43pm UTC](https://discourse.julialang.org/t/string-conversion-from-symbol-with-unicode-does-not-yield-a-string-which-is-intended-to-be-the-same/51289/6 "2020-12-05T14:43:17Z")

</div>

This is very hard to search for, but I’m sure I’ve seen multiple discussions where it was made clear that `:` is not meant to be equivalent to `Symbol`. I think `:` is basically “turn the following into an expression”, which just happens to sometimes coincide with `Symbol` or other types:

```julia
julia> typeof(:1)
Int64

julia> typeof(:a)
Symbol

julia> typeof(:(a+b))
Expr

```

so if you want a symbol, you should probably use `Symbol`

---

<div class="post-metadata">

### Author: ![tomerarnon](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tomerarnon/32/3170_2.png) [@tomerarnon](https://discourse.julialang.org/u/tomerarnon)
#### Post date: [December 5, 2020, 7:13pm UTC](https://discourse.julialang.org/t/string-conversion-from-symbol-with-unicode-does-not-yield-a-string-which-is-intended-to-be-the-same/51289/7 "2020-12-05T19:13:27Z")

</div>

You can use `Unicode.normalize` to get the normalized form

```julia
julia> "ȧ" |> collect
2-element Array{Char,1}:
 'a': ASCII/Unicode U+0061 (category Ll: Letter, lowercase)
 '̇': Unicode U+0307 (category Mn: Mark, nonspacing)

julia> Unicode.normalize("ȧ") |> collect
1-element Array{Char,1}:
 'ȧ': Unicode U+0227 (category Ll: Letter, lowercase)

```
