Python strings question (vertical stack)

D

dmitrey

Hi all,
I have some strings, let it be string1, string2, string3.

So how could String=
"""
string1
string2
string3
"""

be obtained?

Thank you in advance, D.
 
F

Farshid Lashkari

dmitrey said:
Hi all,
I have some strings, let it be string1, string2, string3.

So how could String=
"""
string1
string2
string3
"""

be obtained?

Thank you in advance, D.

If you just want the to add newlines between the strings then you can do
the following:

String = '\\n'.join([string1,string2,string3])

-Farshid
 
D

dmitrey

Thanks all, I have solved the problem:

a="""
%s
%s
%s
""" % ('asdf', 'asdf2', 'asdf3')

print a

D.
 
J

J. Clifford Dyer

Hi all,
I have some strings, let it be string1, string2, string3.

So how could String=
"""
string1
string2
string3
"""

be obtained?

Thank you in advance, D.

If you just want the to add newlines between the strings then you can do
the following:

String = '\\n'.join([string1,string2,string3])

-Farshid

I think you mean '\n'.join([string1,string2,string3])

You actually do want the \ to do its thing in this case.

Cheers,
Cliff
 
F

Farshid Lashkari

J. Clifford Dyer said:
I think you mean '\n'.join([string1,string2,string3])

You actually do want the \ to do its thing in this case.

Yeah, my brain must still be asleep. Thanks for waking it up :)
 
J

John Machin

James Matthews top-posted:
I would say to split it by the newline!

Split what by which newline? The OP appeared to be wanting some kind of
UNsplit ...
On Nov 20, 2007 10:59 PM, John Machin <[email protected]

J. Clifford Dyer said:
I think you mean '\n'.join([string1,string2,string3])
You actually do want the \ to do its thing in this case.

Yeah, my brain must still be asleep. Thanks for waking it up :)

You're not alone :)
Early indications of a pandemic??
... x
... y
... z
... """
a '\nx\ny\nz\n'
'\n'.join(['x', 'y', 'z']) 'x\ny\nz'
 
D

Diez B. Roggisch

dmitrey said:
Thanks all, I have solved the problem:

a="""
%s
%s
%s
""" % ('asdf', 'asdf2', 'asdf3')

print a

This is more expensive and not as flexible as the join method. At least
for growing string-lengths and number of arguments.

Diez
 
J

J. Clifford Dyer

J. Clifford Dyer said:
I think you mean '\n'.join([string1,string2,string3])
You actually do want the \ to do its thing in this case.

Yeah, my brain must still be asleep. Thanks for waking it up :)

You're not alone :)
... x
... y
... z
... """
a '\nx\ny\nz\n'
'\n'.join(['x', 'y', 'z']) 'x\ny\nz'

It's true: my solution did not match the exact specification given by
the OP's code, but 1) I was responding to Farshid Lashkari's slip and 2)
It does what the OP was ultimately asking for--it stacks the results
vertically. My brain was not asleep, I was just not interested in the
other, largely irrelevant part of the OP's question. If he wants a new
line at the beginning and end, I'm sure he can figure out how to do it.
I don't need to jump through those hoops for him.

Cheers,
Cliff
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,274
Messages
2,571,367
Members
48,060
Latest member
JerrodSimc

Latest Threads

Top