472,988 Members | 2,577 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

How to insert in a string @ a index

Hi;

I'm trying to insert XYZ before a keyword in a string. The first and
the last occurence of hello in the string t1 (t1="hello world hello.
hello \nwhy world hello") are keywords. So after the insertion of XYZ
in this string, the result should be t1 = "XYZhello world hello. hello
\nwhy world XYZhello"

The python doesn't supports t1[keyword_index]="XYZhello" (string
object assignment is not supported). How do I get to this problem? Any
sugguestions?

-a.m.

Sep 8 '07 #1
9 1786
The python doesn't supports t1[keyword_index]="XYZhello" (string
object assignment is not supported). How do I get to this problem? Any
sugguestions?
Build a new string var using slicing. eg:

t1 = t1[:keyword_index] + "XYZhello" + [keyword_index]

Or use string formatting:

t1 = "your text bla bla %s bla bla bla %s bla bla" % (string1, string2)
Sep 8 '07 #2
On 9/8/07, David <wi******@gmail.comwrote:
The python doesn't supports t1[keyword_index]="XYZhello" (string
object assignment is not supported). How do I get to this problem? Any
sugguestions?

Build a new string var using slicing. eg:

t1 = t1[:keyword_index] + "XYZhello" + [keyword_index]
Typo. Missed a colon. Should be:

t1 = t1[:keyword_index] + "XYZhello" + [keyword_index:]
Sep 8 '07 #3
Same solution as above, but if you just want "Hello" and to not
include words containing "Hello", i.e. "Helloing" or "Unhello", then
you want to include a leading and/or trailing space.

lit=" hello" ## note added space
t1="nothello world hello. hello \nwhy world hello"
start = t1.find(lit)
t2 = t1[:start+1] + " XYZ" + t1[start+1:]
print "t1 =", t1
print "t2 =", t2
start = t2.rfind(lit)
t3 = t2[:start+1] + " XYZ" + t2[start+1:]
print "t3 =", t3

Sep 8 '07 #4
On 9/8/07, Zentrader <ze********@gmail.comwrote:
Same solution as above, but if you just want "Hello" and to not
include words containing "Hello", i.e. "Helloing" or "Unhello", then
you want to include a leading and/or trailing space.
You can also use the re (regular expression) module to search for
"hello" and make sure it's a complete word. You would use re to search
for something like "\bhello\b". "\b" being regex for an empty string
that only occurs at the start or beginning of a word.

More information on python regexes here: http://www.amk.ca/python/howto/regex/

Here is an example from that page:
>>p = re.compile(r'\bclass\b')
print p.search('no class at all')
<re.MatchObject instance at 80c8f28>
>>print p.search('the declassified algorithm')
None
>>print p.search('one subclass is')
None
Sep 8 '07 #5
lo*****@gmail.com a écrit :
Hi;

I'm trying to insert XYZ before a keyword in a string.
Then forget about it. Python's strings are immutable.

(snip)
The python doesn't supports t1[keyword_index]="XYZhello" (string
object assignment is not supported). How do I get to this problem? Any
sugguestions?
Build a new string.

Sep 9 '07 #6
Thanks guys for you help. I ended up doing this way (for the
records)...

t1 = "hello world hello. hello. \nwhy world hello"

while index<t1.count("hello"):

if (your condition to determine keyword):
t2=t1[:(index+offset)].replace("hello","XYZhello")+t1[((index
+offset):] # offset is 5 (hello = 5 characters)

index+=1

Sep 10 '07 #7
Ant
On Sep 10, 3:15 am, "a.m." <lolu...@gmail.comwrote:
Thanks guys for you help. I ended up doing this way (for the
records)...

t1 = "hello world hello. hello. \nwhy world hello"
....

Another approach may be to use the re module's sub function:

import re

t1 = 'hello world hello. hello. \nwhy world hello'

def matchHandler(match):
if <test here>:
return "XYZ" + match.group(0)
else:
return match.group(0)

re.sub(keyword, matchHandler, t1)

The nice thing about this approach is that you could store keyword:
test_function pairs in a dictionary, and reuse this whole block of
code for arbitrary keywords with arbitrary rules.

--
Ant...

Sep 10 '07 #8
On Mon, 10 Sep 2007 00:29:50 -0700, Ant wrote:
On Sep 10, 3:15 am, "a.m." <lolu...@gmail.comwrote:
>Thanks guys for you help. I ended up doing this way (for the
records)...

t1 = "hello world hello. hello. \nwhy world hello"
...

Another approach may be to use the re module's sub function:

import re

t1 = 'hello world hello. hello. \nwhy world hello'

def matchHandler(match):
if <test here>:
return "XYZ" + match.group(0)
else:
return match.group(0)

re.sub(keyword, matchHandler, t1)
This doesn't work because according to the OP not all 'hello's are
keywords. It seems to depend on the position if it's a keyword or not.

Ciao,
Marc 'BlackJack' Rintsch
Sep 10 '07 #9
On Sep 8, 11:02 am, lolu...@gmail.com wrote:
Hi;

I'm trying to insert XYZ before a keyword in a string. The first and
the last occurence of hello in the string t1 (t1="hello world hello.
hello \nwhy world hello") are keywords. So after the insertion of XYZ
in this string, the result should be t1 = "XYZhello world hello. hello
\nwhy world XYZhello"

The python doesn't supports t1[keyword_index]="XYZhello" (string
object assignment is not supported). How do I get to this problem? Any
sugguestions?
Yet another solution using re
>>t1 = 'hello world hello. hello. \nwhy world hello'
>>import re
l1 = re.split('hello', t1)
l1[0] = 'XYZ' + l1[0]
l1[-2] += 'XYZ'
'hello'.join(l1)
'XYZhello world hello. hello. \nwhy world XYZhello'
>>>
If there are less than two 'hello', you'll get exception and needs
special handling.

Karthik
>
-a.m.
Sep 11 '07 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Philip Boonzaaier | last post by:
I want to be able to generate SQL statements that will go through a list of data, effectively row by row, enquire on the database if this exists in the selected table- If it exists, then the colums...
1
by: Chris Fink | last post by:
I am receiving xml documents from a customer without a reference to a doctype. I know what the Doctype DTD should be need to insert the declaration as follows <?xml version="1.0"...
3
by: Hai Nguyen | last post by:
Hi all I was attempting to insert multiple row by using a loop into a database.A table has 2 primary keys and one regular field (PR) (PR) ID Project Ans 1 2 a 1 ...
3
by: | last post by:
I'm picking up an 'IMPORTS' error for a simple database insert based on two input entry boxes in my form? It says an 'Imports' statement must preceede any declarations....... is this perahps the...
0
by: bdhassan87 | last post by:
how can i insert data to datagrid from text box using visual basic 6.0
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.