469,622 Members | 1,462 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,622 developers. It's quick & easy.

how to prepend string to a string?

so I know you can append a string. But how do you *prepend* a string,
as shown in the following code

#dirList = ['depth1','depth2','depth3']
#string = """position"""
#for x in len(dirList):
# string += '>> %s'%dirList.pop() #(????????????)
#

to return
position>> depth1 >> depth2 >> depth3

rather than
position>> depth3 >> depth2 >> depth1
I really hope this feature exists =)

-thanks

Jul 19 '05 #1
6 25759
flamesrock wrote:
so I know you can append a string. But how do you *prepend* a string,
as shown in the following code

#dirList = ['depth1','depth2','depth3']
#string = """position"""
#for x in len(dirList):
# string += '>> %s'%dirList.pop() #(????????????)
#


How about string = whatever + string?

Note that recommended form is to build a list of strings and then use
''.join(all_my_strings) to form the final result.

After saying all that, here's a better way:

dirList = ['depth1','depth2','depth3', 'position']
print ' >> '.join(dirList)
--
Benji York
Jul 19 '05 #2
flamesrock wrote:
so I know you can append a string. But how do you *prepend* a string,
as shown in the following code

#dirList = ['depth1','depth2','depth3']
#string = """position"""
#for x in len(dirList):
# string += '>> %s'%dirList.pop() #(????????????)


There's not _really_ anything wrong with this code, provided the length
of dirList is "short" (since otherwise the performance issues of growing
strings with += may cause trouble). It might also be preferable to
change from the for statement to a "while dirList:" since that doesn't
require the unused "x" and the confusion of apparently testing the
length of a list that is getting shorter with each iteration.

-Peter
Jul 19 '05 #3
flamesrock wrote:
so I know you can append a string. But how do you *prepend* a string,
as shown in the following code

#dirList = ['depth1','depth2','depth3']
#string = """position"""
#for x in len(dirList):
# string += '>> %s'%dirList.pop() #(????????????)
#

to return
position>> depth1 >> depth2 >> depth3


ISTM the problem is not prepending, but getting the elements of dirList in the order you want. If
you iterate over the list normally you get the desired result:
dirList = ['depth1','depth2','depth3']
string = """position"""
for x in dirList:
string += '>> %s'% x

Kent
Jul 19 '05 #4
Thanks for all the responses :)

You're right, Kent. That does exactly what I need it to.

Anyways.. if there isn't a prepend function, I'm going to request it
from the devs. Would be cool to see that in future versions of python.

Jul 19 '05 #5
flamesrock wrote:
Thanks for all the responses :)

You're right, Kent. That does exactly what I need it to.

Anyways.. if there isn't a prepend function, I'm going to request it
from the devs. Would be cool to see that in future versions of python.


Since strings cannot be modified in-place (they are "immutable"), the
concept of prepend for a string is meaningless (and so is append).
Instead, you simply build a new string from other bits, and it's
entirely up to you which comes first and which comes second (thus
providing implementations equivalent to prepend or append, as you need).

-Peter
Jul 19 '05 #6
How about this:

dirList = ['depth1', 'depth2', 'depth3']
string = """position"""

for x in range(len(dirList)):
string += '>> %s' % dirList.pop(0)

print string

flamesrock wrote:
Thanks for all the responses :)

You're right, Kent. That does exactly what I need it to.

Anyways.. if there isn't a prepend function, I'm going to request it
from the devs. Would be cool to see that in future versions of python.


Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by gipsy boy | last post: by
1 post views Thread by Leigh Finch | last post: by
2 posts views Thread by drjay1627 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.