tags:

views:

68

answers:

4
dbH = []
string = ("%s/t%s/t%s/t%s") % (i1, i2, i3, i4)

originally, 
i1 = blah
i2 = doubleblah
i3 = tripleblah
i4 = 0

...

how can I replace the value in i4 later on? ex. right now dbH[1] = ("Adam\tJack\tJill\tNULL") later on dbH[1] = ("Adam\tJack\tJill\tJohn")

A: 

if i4 is reassigned: string = ("%s/t%s/t%s/t%s") % (i1, i2, i3, i4) dbH[1] = newVal

dougvk
+2  A: 
(i1, i2, i3, i4)

is a tuple and therefore immutable by definition, so it can't be changed.

Use a list:

mylist = [i1, i2, i3, i4]

and then do

mystring = "%s/t%s/t%s/t%s" % tuple(mylist)

Then you can change mylist[3] to anything you want and redo the previous step.

Tim Pietzcker
Thanks for the thorough explanation
@Tim apparently you're a mind-reader.
LarsH
A: 

Do you mean how to get i1, i2, i3, and i4 back once they are in the string?

(i1, i2, i3, i4) = string.split("\t")
Karl Bielefeldt
+1  A: 

To handle this nicely, you got 2 options:
1. Use

TEMPLATE = "%s/t%s/t%s/t%s"

and then recreate the string when you need it by:

string = TEMPLATE % your_tuple


2. Create a function that will return the most up-to-date string based on the current i4. Use it around in your code instead of directly calling "string".

yulkes