4 Replies - 9937 Views - Last Post: 08 October 2010 - 07:42 AM Rate Topic: -----

#1 gretty  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 123
  • Joined: 25-May 09

String Functions: Replace between an index & add at index

Posted 07 October 2010 - 09:23 PM

Hello

This is kinof of a simple question but not :P

I am trying to understand if there are any string functions that can allow myself to alter strings by passing the index's of characters (alot of the python string functions take characters or strings as parameters not the indexes):

- Change the text with in a string from x index to y index to the string z...

so for example; if I have the string

a = "hello"
a.replace( 1, 4, "ummer" ) # change the characters from a[1] to a[4] to "ummer"

print a # so a will now equal "hummero"


- insert a string into another string...

for example

a = hello
a.insert( 1, "aaaaaa" )

print a # so a will now equal "heaaaaaallo"


I know I can replace stuff saying this... a.replace( 'ell', 'ummer' ) but I need to pass the index of the string rather than the characters as the parameters

Is This A Good Question/Topic? 0
  • +

Replies To: String Functions: Replace between an index & add at index

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2876
  • View blog
  • Posts: 11,050
  • Joined: 15-July 08

Re: String Functions: Replace between an index & add at index

Posted 07 October 2010 - 09:35 PM

How about String indexes and concatenation:

startIndex = 2
endIndex = 4
replace = "AaaaaaA"

string = oldStr[0:startIndex] + "AaaaaaaA" + oldStr[endIndex+1:]



Now the variable String has the new replaced String
Was This Post Helpful? 0
  • +
  • -

#3 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 797
  • Joined: 08-June 10

Re: String Functions: Replace between an index & add at index

Posted 08 October 2010 - 06:38 AM

I'm not sure how intently you worded your question, but strings in Python are immutable. This means that a string object cannot be modified in place; rather, you'll need to reassign it, as Dogstopper did.

As you see above, performing string manipulations is trivial in Python. You can simply write your string manipulator functions and overwrite your strings:
def replace(index1, index2, mainstring, replacementstring):
    return mainstring.replace(mainstring[index1:index2], replacementstring)

def insert(index, mainstring, insertstring):
    return mainstring[:index] + insertstring + mainstring[index:]

thestring = "abcdefg"
thestring = replace(2, 3, thestring, "wow")
thestring = insert(3, thestring, "hah")


This post has been edited by Motoma: 08 October 2010 - 07:38 AM

Was This Post Helpful? 0
  • +
  • -

#4 Nallo  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 163
  • View blog
  • Posts: 255
  • Joined: 19-July 09

Re: String Functions: Replace between an index & add at index

Posted 08 October 2010 - 07:34 AM

Finally I find motoma making a little mistake :whistling:
In line 8 and 9 of his code he forgot to pass thestring as argument to the replace and insert functions. It should read:
thestring = replace(2, 3, thestring, "wow")
thestring = insert(3, thestring, "hah")


This post has been edited by Nallo: 08 October 2010 - 07:35 AM

Was This Post Helpful? 1
  • +
  • -

#5 Motoma  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 452
  • View blog
  • Posts: 797
  • Joined: 08-June 10

Re: String Functions: Replace between an index & add at index

Posted 08 October 2010 - 07:42 AM

Hey thanks Nallo! Good eye! You were right to double check my code; I rarely write the smaller snippets in an IDE, rather, I write the code directly in DIC. This can lead to the occasional mistake :innocent:

Thanks for catching my bad behavior :oops:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1