473,394 Members | 1,932 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,394 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 1799
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
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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,...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.