Why is julia interpreting what's inside a string?

490 views
Skip to first unread message

J Luis

unread,
Sep 6, 2015, 7:08:30 PM9/6/15
to julia-users
I need to create these, not to interpret what julia thinks it means.

julia> "3.2 3.6 z(x,y) = x@~\327@~exp(-x@+2@+-y@+2@+)"
ERROR: syntax: invalid UTF-8 sequence

julia> "grdmath $ DDX"
ERROR: syntax: invalid interpolation syntax: "$ "

How do I do that?

Thanks

Leah Hanson

unread,
Sep 6, 2015, 7:19:03 PM9/6/15
to Julia Users
Julia has some special characters it recognizes inside string. For example, `$` lets you interpolate Julia variables or expressions inside a string. To get a string that represents what you've literally typed in, you need to add a backslash before the backslash and dollar sign characters. The backslash will not be present when you print the strings out or otherwise use them, although it will appear when Julia shows the value to you at the REPL.

~~~

julia> "3.2 3.6 z(x,y) = x@~\\327@~exp(-x@+2@+-y@+2@+)"
"3.2 3.6 z(x,y) = x@~\\327@~exp(-x@+2@+-y@+2@+)"

julia> print(ans)

3.2 3.6 z(x,y) = x@~\327@~exp(-x@+2@+-y@+2@+)

julia> "grdmath \$ DDX"
"grdmath \$ DDX"

julia> print(ans)
grdmath $ DDX
~~~

-- Leah

J Luis

unread,
Sep 6, 2015, 7:26:36 PM9/6/15
to julia-users
Thanks.
I find this REPL behavior a bit confusing.

Sean Marshallsay

unread,
Sep 7, 2015, 2:35:08 PM9/7/15
to julia-users
Note that if you need the equivalent of Python's raw strings you can just use an empty macro

julia> macro raw_str(s) s end

julia> raw"3.2 3.6 z(x,y) = x@~\327@~exp(-x@+2@+-y@+2@+)"
"3.2 3.6 z(x,y) = x@~\\327@~exp(-x@+2@+-y@+2@+)"

julia> raw"grdmath $ DDX"
"grdmath \$ DDX"

julia>
Reply all
Reply to author
Forward
0 new messages