June 2002
Beginner to intermediate
640 pages
14h 24m
English
split(s, [sep], [maxsplit]) and splitfields(s, [sep], [maxsplit]) both split a string into a sequence. With the sep argument you can specify what you want to use for the separator—the default is whitespace (spaces or tabs). The maxsplit optional argument allows you to specify how many items you want to break up; the default is all.
Here, with one line of code, we parse an address containing five fields. (Try to do this with Java, C, Delphi, or Visual Basic—you can't.)
>>> input_string = "Bill,Gates,123 Main St., WA, 65743" >>> fname, lname, street, state, zip = split(input_string,",") >>> print """ ... Name: %(fname)s %(lname)s ... Street: %(street)s ... %(state)s, %(zip)s""" ...
Read now
Unlock full access