# When to use String() over string()?

**URL:** <https://discourse.julialang.org/t/when-to-use-string-over-string/98068>\
**Category:** New to Julia\
**Tags:** question, strings\
**Created:** [April 28, 2023, 11:11pm UTC](https://discourse.julialang.org/t/when-to-use-string-over-string/98068 "2023-04-28T23:11:10Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [April 28, 2023, 11:11pm UTC](https://discourse.julialang.org/t/when-to-use-string-over-string/98068/1 "2023-04-28T23:11:10Z")

</div>

Hi this may be a silly question but just wondering if there are cases when `String()` be used over `string()`? To the untutored, it appears that they are doing the same thing.

```julia
string(SubString("use string")) === String(SubString("use string"))
true

```

Except that `string()` also has a method for integer values. I didn’t grasp other distinctions between the two functions from the docs. But I often do clumsy things with Julia functions, (e.g. using Joins on Floating Point Values) before being corrected here. Any clarifications would be appreciated. Thanks!

---

<div class="post-metadata">

**Author:** ![awasserman](https://avatars.discourse-cdn.com/v4/letter/a/9de0a6/32.png) [@awasserman](https://discourse.julialang.org/u/awasserman)\
**Post date:** [April 28, 2023, 11:33pm UTC](https://discourse.julialang.org/t/when-to-use-string-over-string/98068/2 "2023-04-28T23:33:14Z")

</div>

I would use `String()` when you want to guarantee that what comes out is a standard string object.

For instance, if we were using types from [`InlineStrings`](https://github.com/JuliaStrings/InlineStrings.jl), then we might want to eventually convert them to standard strings if we want to make containers with strings of other types. As shown below, `String()` will output a `String` while `string` (lowercase) will return the original type.

```julia
julia> using InlineStrings
julia> s = String7("asdf")
"asdf"

julia> typeof(s)
String7

julia> typeof(String(s))
String

julia> typeof(string(s))
String7

```

---

<div class="post-metadata">

**Author:** ![wherrera](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/wherrera/32/5288_2.png) [@wherrera](https://discourse.julialang.org/u/wherrera)\
**Post date:** [April 28, 2023, 11:42pm UTC](https://discourse.julialang.org/t/when-to-use-string-over-string/98068/3 "2023-04-28T23:42:13Z")

</div>

One important distinction is that string([‘a’, ‘b’, ‘c’]) is “[‘a’, ‘b’, ‘c’]” but String([‘a’, ‘b’, ‘c’]) is “abc”.

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [April 29, 2023, 12:58am UTC](https://discourse.julialang.org/t/when-to-use-string-over-string/98068/4 "2023-04-29T00:58:51Z")

</div>

Thanks so much for the clarification! In general is there a place to look up such distinctions if one lacks the background to extrapolate the details from the docs? For example if I have Vector of Strings

```julia
string(["a","b","c"])
"[\"a\", \"b\", \"c\"]"

```

whereas `String(["a","b","c"])` without broadcasting returns a `method error`. But I wouldn’t have known this without the instructive Vector of Characters example provided above or just trial and error.

Also why does

```julia
julia> string.(["a","b","c"]) === String.(["a","b","c"])
false

```

whereas the earlier example

```julia
string(SubString("use string")) === String(SubString("use string"))
true

```

I assume this has to do with broadcasting and where the object is stored? Thanks again!

---

<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:** [April 29, 2023, 1:21am UTC](https://discourse.julialang.org/t/when-to-use-string-over-string/98068/5 "2023-04-29T01:21:27Z")

</div>

> [@phantom](#):
>
> distinctions if one lacks the background to extrapolate the details from the docs

distinction is just that `String` is a type, `string` can be named something else like `stringify` or `make_string_for_human_to_read`.

When you call the type `String`, think of it as doing `convert(String, x)`, and the mental model is if you’re representing the (logically) same data, you can do `convert()`, if you’re just getting the human-readable string representation of something, then you’re doing `string()`.

my go to example

```julia
julia> String([0x39, 0x52])
"9R"

julia> string([0x39, 0x52])
"UInt8[0x39, 0x52]"

```

---

<div class="post-metadata">

**Author:** ![phantom](https://avatars.discourse-cdn.com/v4/letter/p/e0b2c6/32.png) [@phantom](https://discourse.julialang.org/u/phantom)\
**Post date:** [April 29, 2023, 3:38am UTC](https://discourse.julialang.org/t/when-to-use-string-over-string/98068/6 "2023-04-29T03:38:11Z")

</div>

Thanks! This was really informative. I think I understand the docs a little better now that you pointed that out. So roughly speaking:

`String(stuff)` is equivalent to `convert(String, stuff)`

whereas

`string(stuff)` is equivalent to `convert` output of `print(stuff)` into type `String`.

Except that when it can’t convert `print(stuff)` e.g. the case above of a Vector of Strings

```julia
print(["a","b","c"])
["a", "b", "c"]

```

where `"["a","b","c"]"` would create a syntax error of juxtaposing a string literal then something like

```julia
string(["a","b","c"])
"[\"a\", \"b\", \"c\"]"

```

is done?

---

<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:** [April 29, 2023, 3:35pm UTC](https://discourse.julialang.org/t/when-to-use-string-over-string/98068/7 "2023-04-29T15:35:03Z")

</div>

> [@phantom](#):
>
> where `"["a","b","c"]"` would create a syntax error of juxtaposing

so `"[\"a\", \"b\", \"c\"]"` is precisely how you have to type a literal string, e.g.

```julia
println("[\"a\", \"b\", \"c\"]")
["a", "b", "c"]

```

---

<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:** [April 29, 2023, 8:28pm UTC](https://discourse.julialang.org/t/when-to-use-string-over-string/98068/8 "2023-04-29T20:28:17Z")

</div>

```julia
String([0x61,0x62,0x63]) =="abc"

```
