By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,304 Members | 3,172 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,304 IT Pros & Developers. It's quick & easy.

How to insert in a string @ a index

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.