Few Things to Note

• You can assign multiple variables to a sequence (Chapter 12).

• Thelocals () built-in function (Chapter 9) returns a arctroaery that contains the variables in a local namespace, so the statement

returns 'Gates'

• The% format string operatot works with dKtionaries oh s eque^^f (Chapter 3).

Here we demonstrate that split () and splitfields () do the same thing:

['Bill,Gates,123', 'Main', 'St.,', 'WA,', '65743'] >>> splitfields(input_string)

['Bill,Gates,123' , 'Main', 'St.,', 'WA,', '65743'] Here's an example demonstrating the default operation for split () :

>>> split("tab\tspace wordl word2 word3\t\t\tword4")

['tab', 'space', 'wordl', 'word2', 'word3', 'word4']

join(words, [sep]) and joinfields(words, [sep] ) also do the same thing. Here's our last example showing how to build an address string from a seqnence of fields:

>>> seq = (fname, lname, street, state, zip)

>>> input_string = join(seq, ",")

>>> print input_string

Bill,Gates,12 3 Main St., WA, 65743

The next two examples demonstrate the similarities of join() and joinfields():

>>> seq = ("1","2","3","4","5")

>>> join (seq, "#") '1#2#3#4 #5'

>>> joinfields(seq,"#") '1#2#3#4 #5'

Was this article helpful?

0 0

Post a comment