473,387 Members | 1,876 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 26486
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: gipsy boy | last post by:
// -- using namespace std; map<string,string> formMap; list<string> formParams; for(list<string>::iterator fit = formParams.begin(); fit!=formParams.end(); fit++) { cout << "key=" << *fit;...
7
by: herrcho | last post by:
i'm in the course of learning C, and found these two words "string, string literal" confusing me.. I'd like to know the difference between them.. Thank you
1
by: Jason Chan | last post by:
in asp.net 2.0, Page.RegisterClientScriptBlock is replaced by Client.RegisterClientScriptBlock function signature: Client.RegisterClientScriptBlock(Type, String, String) what should i put...
5
by: Martin Jørgensen | last post by:
Hello again, Sorry to bother but I guess my C++ book isn't very good since it obviously contains errors so the program code doesn't work with g++. However I don't understand what the problem...
7
by: Wilson | last post by:
Hi, How do get the Dictioanry object from FiedlInfo ? my code : fieldInfo = this.GetType().GetField("dictioanry1"); ??Dictionary<string, string> dicTemp1 = (Dictionary<string,...
2
by: Steve Richter | last post by:
KeyValuePair<string,stringhas a ToString method that returns the KeyValue pair seperated by a comma and enclosed in : Is this method used as a building block for serialization? The reason I...
0
by: Andy B | last post by:
Is there a way to databind a checkBoxList to a Dictionary<string, string> object? If so, how do you do it?
2
by: Andy B | last post by:
I don't know if this is even working or not but here is the problem. I have a gridview that I databound to a dictionary<string, stringcollection: Contract StockContract = new Contract();...
1
by: Leigh Finch | last post by:
Hey Guys, Quick question about the PHP compiler, is there any performance difference between $string .= $new; and $string = $string . $new;
2
by: drjay1627 | last post by:
I need help with inserting a string for both key and value to a map. while(!myHuffFile.eof()){ string word, code; int freq; myHuffFile >> word >> freq >> code; if(word == ""){...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.