# How to do escape\_string

**URL:** <https://discourse.julialang.org/t/how-to-do-escape-string/52401>\
**Category:** New to Julia\
**Tags:** strings\
**Created:** [December 26, 2020, 1:10pm UTC](https://discourse.julialang.org/t/how-to-do-escape-string/52401 "2020-12-26T13:10:47Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Zaim\_Awang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zaim_awang/32/20491_2.png) [@Zaim\_Awang](https://discourse.julialang.org/u/Zaim_Awang)\
**Post date:** [December 26, 2020, 1:10pm UTC](https://discourse.julialang.org/t/how-to-do-escape-string/52401/1 "2020-12-26T13:10:47Z")

</div>

I am trying to use escape\_string because as per documentation, “Backslashes ( `\` ) are escaped with a double-backslash ( `"\\"` )”.

This is what I tried:

`mypath1 = escape_string("C:\Users\User\Dropbox\codes\JULIA\stock1\")`

The error message I get is

`ERROR: LoadError: syntax: invalid escape sequence.`

I am not sure what I did wrong.

---

<div class="post-metadata">

**Author:** ![cce](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cce/32/460_2.png) [@cce](https://discourse.julialang.org/u/cce)\
**Post date:** [December 26, 2020, 1:26pm UTC](https://discourse.julialang.org/t/how-to-do-escape-string/52401/2 "2020-12-26T13:26:46Z")

</div>

> [@Zaim\_Awang](#):
>
> `mypath1 = escape_string("C:\Users\User\Dropbox\codes\JULIA\stock1\")`

So, I’m not sure `escape_string` is what you need. You could try to use raw strings.

`mypath1 = raw"C:\Users\User\Dropbox\codes\JULIA\stock1\"`

Oops! That doesn’t work. I forgot that one needs to add a double slash at the end.

```julia
julia> mypath = raw"C:\Users\User\Dropbox\codes\JULIA\stock1\\"
"C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\"

```

Alternatively, you could just do the escaping manually…

```julia
julia> mypath = "C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\"
"C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\"

```

---

<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:** [December 26, 2020, 1:57pm UTC](https://discourse.julialang.org/t/how-to-do-escape-string/52401/3 "2020-12-26T13:57:27Z")

</div>

> [@Zaim\_Awang](#):
>
> I am trying to use escape\_string because as per documentation, “Backslashes ( `\` ) are escaped with a double-backslash ( `"\\"` )”.
> 
> This is what I tried:
> 
> `mypath1 = escape_string("C:\Users\User\Dropbox\codes\JULIA\stock1\")`

I think you’re misunderstanding `escape_string` — it is not to help you _type_ the literal string, but rather it is there to help you add backslashes to an existing string, primarily for printing.

So, when you type this path as a literal string, you still need the extra backslashes:

```julia
julia> mypath = "C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\"
"C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\"

```

Note that it is displayed between `"..."` as you would type it, with the escaped backslashes, but these are not actually stored in the string, as you can see if you print it:

```julia
julia> println("This is the mypath string: ", mypath)
This is the mypath string: C:\Users\User\Dropbox\codes\JULIA\stock1\

```

You can also enter the same string using `raw"..."` if you want to avoid having to type so many backslashes, although you still to escape the final backslash:

```julia
julia> mypath2 = raw"C:\Users\User\Dropbox\codes\JULIA\stock1\\"
"C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\"

julia> mypath2 == mypath
true

```

As mentioned above, `escape_string` actually adds the backslash escapes into the string, which looks a bit weird if you display it because `\\` is displayed escaped as `\\\\`:

```julia
julia> escape_string(mypath)
"C:\\\\Users\\\\User\\\\Dropbox\\\\codes\\\\JULIA\\\\stock1\\\\"

```

The reason for `escape_string` is more apparent if you print it:

```julia
julia> println("This is the mypath string WITH ESCAPES: ", escape_string(mypath))
This is the mypath string WITH ESCAPES: C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\

```

[Some form of escaping](https://en.wikipedia.org/wiki/String_literal#Escape_sequences) is used for literal strings in [every programming language](https://rosettacode.org/wiki/Literals/String), because of the inevitable need to distinguish between language syntax and the contents of the string for things like string delimiters. [xkcd](https://xkcd.com/1638/):  
 ![](https://global.discourse-cdn.com/julialang/original/3X/a/5/a53167e2b3f78b38c75aebb874be1a74a2eff0b3.png)

---

<div class="post-metadata">

**Author:** ![Zaim\_Awang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zaim_awang/32/20491_2.png) [@Zaim\_Awang](https://discourse.julialang.org/u/Zaim_Awang)\
**Post date:** [December 26, 2020, 2:08pm UTC](https://discourse.julialang.org/t/how-to-do-escape-string/52401/4 "2020-12-26T14:08:46Z")

</div>

Thanks for the lenghty explanation. I actually started with

mypath = “C:\Users\User\Dropbox\codes\JULIA\stock1\”`

but I recognize the _user_ may not always add the extra backslash

My intent is this  
`mydf = DataFrame(CSV.File(mypath))`

given if

`mypath = "C:\Users\User\Dropbox\codes\JULIA\stock1\"`

Assuming mypath is not changed, how?  
If I have to use raw, I have to add that extra backslash to mypath

but  
`mypath2 = mypath*"\"`

doesn’t work, and adding raw in between too somehow doesn’t work. I tried parsing, but that made it worse, I am so new to this 😅

---

<div class="post-metadata">

**Author:** ![cce](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cce/32/460_2.png) [@cce](https://discourse.julialang.org/u/cce)\
**Post date:** [December 26, 2020, 2:10pm UTC](https://discourse.julialang.org/t/how-to-do-escape-string/52401/5 "2020-12-26T14:10:32Z")

</div>

I think that `CSV.File` requires a filepath, not a directory. So, perhaps you could use the filename?

```julia
myfile = "C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\myfile.csv"
mydf = DataFrame(CSV.File(myfile))

```

In this case, since you don’t have a trailing slash, raw string works well…

```julia
myfile = raw"C:\Users\User\Dropbox\codes\JULIA\stock1\myfile.csv"
mydf = DataFrame(CSV.File(myfile))

```

---

<div class="post-metadata">

**Author:** ![Zaim\_Awang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zaim_awang/32/20491_2.png) [@Zaim\_Awang](https://discourse.julialang.org/u/Zaim_Awang)\
**Post date:** [December 26, 2020, 2:20pm UTC](https://discourse.julialang.org/t/how-to-do-escape-string/52401/6 "2020-12-26T14:20:50Z")

</div>

Thanks!

```julia
mypath = "C:\Users\User\Dropbox\codes\JULIA\stock1\TRAINING\IBM.csv"
myfile = Meta.parse(raw*mypath)
mydf = DataFrame(CSV.File(myfile))

```

May I ask, how do I correct the above?

```julia
mypath = "C:\Users\User\Dropbox\codes\JULIA\stock1\TRAINING\IBM.csv"
myfile = Meta.parse("raw"*mypath)
mydf = DataFrame(CSV.File(myfile))

```

The above doesn’t work as well

---

<div class="post-metadata">

**Author:** ![cce](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cce/32/460_2.png) [@cce](https://discourse.julialang.org/u/cce)\
**Post date:** [December 26, 2020, 2:21pm UTC](https://discourse.julialang.org/t/how-to-do-escape-string/52401/7 "2020-12-26T14:21:49Z")

</div>

```julia
myfile = raw"C:\Users\User\Dropbox\codes\JULIA\stock1\TRAINING\IBM.csv"
mydf = DataFrame(CSV.File(myfile))

```

Why do you think you need `Meta.parse`?

---

<div class="post-metadata">

**Author:** ![Zaim\_Awang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zaim_awang/32/20491_2.png) [@Zaim\_Awang](https://discourse.julialang.org/u/Zaim_Awang)\
**Post date:** [December 26, 2020, 2:23pm UTC](https://discourse.julialang.org/t/how-to-do-escape-string/52401/8 "2020-12-26T14:23:26Z")

</div>

I am assuming anything that is not a variable need to be in quotation if it is in parse.

This meta.parse works

```julia
ex1 = Meta.parse("DataFrame(yahoo("*b*", YahooOpt(period1 = startday, period2 = endday)))") 
mydf1 = eval(ex1)

```

---

<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:** [December 26, 2020, 2:24pm UTC](https://discourse.julialang.org/t/how-to-do-escape-string/52401/9 "2020-12-26T14:24:06Z")

</div>

Should be

```julia
mypath = raw"C:\Users\User\Dropbox\codes\JULIA\stock1\TRAINING\IBM.csv"
mydf = DataFrame(CSV.File(myfile))

```

You need `raw` (or backslash escapes) when you enter the string in your code.

---

<div class="post-metadata">

**Author:** ![cce](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cce/32/460_2.png) [@cce](https://discourse.julialang.org/u/cce)\
**Post date:** [December 26, 2020, 2:24pm UTC](https://discourse.julialang.org/t/how-to-do-escape-string/52401/10 "2020-12-26T14:24:10Z")

</div>

I think you’re over thinking it. Do the 2 lines above work?

---

<div class="post-metadata">

**Author:** ![Zaim\_Awang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zaim_awang/32/20491_2.png) [@Zaim\_Awang](https://discourse.julialang.org/u/Zaim_Awang)\
**Post date:** [December 26, 2020, 2:26pm UTC](https://discourse.julialang.org/t/how-to-do-escape-string/52401/11 "2020-12-26T14:26:33Z")

</div>

> [@Zaim\_Awang](#):
>
> `mypath = "C:\Users\User\Dropbox\codes\JULIA\stock1\TRAINING\IBM.csv"`

Thanks. If  
`mypath = "C:\Users\User\Dropbox\codes\JULIA\stock1\TRAINING\IBM.csv"`  
remains, how do I mix this raw?

---

<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:** [December 26, 2020, 2:28pm UTC](https://discourse.julialang.org/t/how-to-do-escape-string/52401/12 "2020-12-26T14:28:02Z")

</div>

> [@Zaim\_Awang](#):
>
> Thanks for the lenghty explanation. I actually started with
> 
> ```julia
> mypath = “C:\Users\User\Dropbox\codes\JULIA\stock1\”`
> 
> ```
> 
> but I recognize the _user_ may not always add the extra backslash

Who is the “user” here? Are you typing the path, or are you reading it from some external source?

(The above won’t work because `mypath = "..."` is Julia code and needs to follow Julia syntax — you need to escape backslashes or use `raw"..."`. I’m not sure you know what escaping actually means yet?)

---

<div class="post-metadata">

**Author:** ![cce](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/cce/32/460_2.png) [@cce](https://discourse.julialang.org/u/cce)\
**Post date:** [December 26, 2020, 2:28pm UTC](https://discourse.julialang.org/t/how-to-do-escape-string/52401/13 "2020-12-26T14:28:43Z")

</div>

To assign that string to a variable in Julia, you have two options. Either you manually escape each slash by doubling it…

```julia
mypath = "C:\\Users\\User\\Dropbox\\codes\\JULIA\\stock1\\TRAINING\\IBM.csv"

```

or you use raw string…

```julia
mypath = raw"C:\Users\User\Dropbox\codes\JULIA\stock1\TRAINING\IBM.csv"

```

In Julia string literals, the backslash has special meaning so that you can add non-printable characters (such as the newline) or, include the double quote.

```julia
julia> print("The sun came over the horizon.\nHe said: \"Beautiful!\"")
The sun came over the horizon.
He said: "Beautiful!"

```

This is why using slash in a string literal is complex. So, to actually include a slash in your output string, you have to double it.

Anyway, I’d avoid `eval` and `Meta.parse` – it’s exceptionally rare you’d ever need to use them. I’ve been using Julia for years in some complicated works and have found only a few cases where `Meta.parse` and `eval` were indicated.

---

<div class="post-metadata">

**Author:** ![Zaim\_Awang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zaim_awang/32/20491_2.png) [@Zaim\_Awang](https://discourse.julialang.org/u/Zaim_Awang)\
**Post date:** [December 26, 2020, 2:32pm UTC](https://discourse.julialang.org/t/how-to-do-escape-string/52401/14 "2020-12-26T14:32:01Z")

</div>

The plan is it will be an external source in the future.

Maybe I have not intuitively understand what escaping is.  
But the way I understand it, I do need the double backslash to make it a real backslash.

---

<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:** [December 26, 2020, 2:41pm UTC](https://discourse.julialang.org/t/how-to-do-escape-string/52401/15 "2020-12-26T14:41:16Z")

</div>

> [@Zaim\_Awang](#):
>
> The plan is it will be an external source in the future.

If the external source is not Julia code (e.g. you are reading the filename using `read` or from a file-picker prompt), then the user won’t need extra backslashes. Escaping is only needed when strings are _entered literally in Julia code_.

> [@Zaim\_Awang](#):
>
> Maybe I have not intuitively understand what escaping is.  
> But the way I understand it, I do need the double backslash to make it a real backslash.

…_in Julia code_. The point is that when you have a string `"...."` in Julia code (a [string literal](https://en.wikipedia.org/wiki/String_literal)), this is only a _representation_ of the actual contents of the string, and is not the same as the string itself as it is stored in memory. A backslash in the underlying string is _represented_ in _Julia code_ by a string literal with an escaped (backslashed) backslash (a double backslash).

In the same way, if you want a string with a quotation mark, you need to _represent it_ in Julia code with an escaped backslash: `"a quote \" in a string"`. This is, in some sense, the _origin_ of escaping: if you didn’t mark (_escape_) the quotation mark in some special way, Julia couldn’t distinguish it from the end of the string. But then once you settle on `\"` as the way to escape a quote `"`, then you need a way to escape the backslash itself.

Again, this is not specific to Julia. _Every_ programming language has some version of string escaping.

---

<div class="post-metadata">

**Author:** ![Zaim\_Awang](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/zaim_awang/32/20491_2.png) [@Zaim\_Awang](https://discourse.julialang.org/u/Zaim_Awang)\
**Post date:** [December 26, 2020, 3:41pm UTC](https://discourse.julialang.org/t/how-to-do-escape-string/52401/16 "2020-12-26T15:41:56Z")

</div>

Thanks! I will try reading the path from a file.
