how to prepend string to a string?

F

flamesrock

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
 
B

Benji York

flamesrock said:
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)
 
P

Peter Hansen

flamesrock said:
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
 
K

Kent Johnson

flamesrock said:
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
 
F

flamesrock

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.
 
P

Peter Hansen

flamesrock said:
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
 
N

newcoder

How about this:

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

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

print string
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top