By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,550 Members | 1,161 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,550 IT Pros & Developers. It's quick & easy.

Python strings question (vertical stack)

P: n/a
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.
Nov 20 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a
dmitrey wrote:
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
Nov 20 '07 #2

P: n/a
Thanks all, I have solved the problem:

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

print a

D.
Nov 20 '07 #3

P: n/a
On Tue, Nov 20, 2007 at 11:40:59AM -0800, Farshid Lashkari wrote regarding Re: Python strings question (vertical stack):
>
dmitrey wrote:
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
--
http://mail.python.org/mailman/listinfo/python-list
I think you mean '\n'.join([string1,string2,string3])

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

Cheers,
Cliff
Nov 20 '07 #4

P: n/a
J. Clifford Dyer wrote:
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 :)
Nov 20 '07 #5

P: n/a
On Nov 21, 7:15 am, Farshid Lashkari <n...@spam.comwrote:
J. Clifford Dyer wrote:
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 :-)
>>a = """
.... x
.... y
.... z
.... """
>>a
'\nx\ny\nz\n'
>>'\n'.join(['x', 'y', 'z'])
'x\ny\nz'
>>>
Nov 20 '07 #6

P: n/a
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 <sj******@lexicon.net
<mailto:sj******@lexicon.net>wrote:

On Nov 21, 7:15 am, Farshid Lashkari <n...@spam.com
<mailto:n...@spam.com>wrote:
J. Clifford Dyer wrote:
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??
>
>>a = """
... x
... y
... z
... """
>>a
'\nx\ny\nz\n'
>>'\n'.join(['x', 'y', 'z'])
'x\ny\nz'
>>>

Nov 20 '07 #7

P: n/a
dmitrey schrieb:
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
Nov 20 '07 #8

P: n/a
On Tue, 2007-11-20 at 13:59 -0800, John Machin wrote:
On Nov 21, 7:15 am, Farshid Lashkari <n...@spam.comwrote:
J. Clifford Dyer wrote:
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 :-)
>a = """
... 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

Nov 21 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.