473,657 Members | 2,735 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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','depth 2','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 26502
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','depth 2','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','depth 2','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','depth 2','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','depth 2','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','depth 2','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(dirLi st)):
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
6709
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; formMap.insert(make_pair(*fit,*(++fit)));
7
7069
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
3181
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 on the Type?
5
7221
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 is. Last time the problem was that "using namespace std" apparently had a "link" class/object defined already. Now I get: error: no matching function for call to 'String::String(String)'
7
8711
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, string>)fieldInfo; Thanks Wilson
2
6522
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 ask is because I dont see a "FromString" or "Parse" static method that takes a string as an argument and returns a KeyValuePair<string,stringobject.
0
1326
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
3129
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(); StockContract.Dictionary = ContractDictionary<string, string>(); GridView1.DataSource=StockContract.Dictionary; So far so good. Now I assign something to the Dictionary collection through some textboxes and a button:
1
1353
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
1818
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 == ""){ break;
0
8407
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8739
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8512
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6175
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5638
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4329
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2739
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1969
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.