string literals split to multilines?

1,051 views
Skip to first unread message

Andreas Lobinger

unread,
Jan 6, 2015, 5:15:13 AM1/6/15
to julia...@googlegroups.com
Hello colleagues,

is there a counterpart for the string literal split to multiple lines like in python?

d = '09ab\
eff1\
a2a4'

Wishing a happy day,
       Andreas

René Donner

unread,
Jan 6, 2015, 5:19:37 AM1/6/15
to julia...@googlegroups.com
hi,

this should work:

d = """aaa
bbb
ccc"""

Rene

Mike Nolta

unread,
Jan 6, 2015, 1:36:40 PM1/6/15
to julia...@googlegroups.com
On Tue, Jan 6, 2015 at 5:19 AM, René Donner <li...@donner.at> wrote:
> d = """aaa
> bbb
> ccc"""

I think what Andreas wanted was a string without newlines. At the
moment, you can't do that in julia.

-Mike

Steven G. Johnson

unread,
Jan 6, 2015, 2:11:47 PM1/6/15
to julia...@googlegroups.com


On Tuesday, January 6, 2015 5:15:13 AM UTC-5, Andreas Lobinger wrote:
Hello colleagues,

is there a counterpart for the string literal split to multiple lines like in python?

d = '09ab\
eff1\
a2a4'

You can always just concatenate:

d = "09ab" *
      "eff1" *
      "a2a4" 

Sean Marshallsay

unread,
Jan 6, 2015, 2:26:55 PM1/6/15
to julia...@googlegroups.com
Rene, multiline literals should also work with just a single quote.

julia> d = "aaa
       bbb
       ccc"
"aaa\nbbb\nccc"

Andreas Lobinger

unread,
Jan 7, 2015, 1:19:59 AM1/7/15
to julia...@googlegroups.com
Thanks for all the answers. As Mike Nolta recognized correctly i wanted to split the string over a few lines without including the resp. newlines. In this particular case - i'm including a .svg file into a string for testing .svg import - the additional whitespace (incl. newlines) doesn't matter.

Ronald L. Rivest

unread,
Jan 7, 2015, 1:42:55 PM1/7/15
to julia...@googlegroups.com
Why not use concatenation:
   d = "aaa" *
         "bbb" *
         "ccc"
??
Cheers,
Ron

Ismael VC

unread,
Jan 9, 2015, 2:56:54 PM1/9/15
to julia...@googlegroups.com
Note that in Python if there is whitespace after the `\` it will be an error:

In [1]: s = 'one\
  File "<ipython-input-1-270c132ab6d9>", line 1
    s = '
one\
             
^
SyntaxError: EOL while scanning string literal

I think it's safer to just use the implicit string literal concatenation and implicit line continuation:

In [2]: s = (
   
...:     'one'
   
...:     'two'
   
...:     'tree'
   
...: )

In [3]: s
Out[3]: 'onetwotree'

Which lets you order the strings with the desired indentation level, without forcing you to write them flush to the left, as in your example.
Reply all
Reply to author
Forward
0 new messages