From 362f9d3f4cee73cc3c605737e84949cb5fbada10 Mon Sep 17 00:00:00 2001 From: Mark Pilgrim Date: Thu, 28 Jan 2010 14:11:57 -0500 Subject: [PATCH] call out optional second argument to split() --- strings.html | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/strings.html b/strings.html index 2e8780d..188fa6d 100755 --- a/strings.html +++ b/strings.html @@ -261,8 +261,8 @@ experience of years. {'password': 'PapayaWhip', 'user': 'pilgrim', 'database': 'master'}
    -
  1. The split() string method takes one argument, a delimiter, and splits a string into a list of strings based on the delimiter. Here, the delimiter is an ampersand character, but it could be anything. -
  2. Now we have a list of strings, each with a key, followed by an equals sign, followed by a value. We can use a list comprehension to iterate over the entire list and split each string into two strings based on the first equals sign. (In theory, a value could contain an equals sign too. If we just used 'key=value=foo'.split('='), we would end up with a three-item list ['key', 'value', 'foo'].) +
  3. The split() string method has one required argument, a delimiter. The method splits a string into a list of strings based on the delimiter. Here, the delimiter is an ampersand character, but it could be anything. +
  4. Now we have a list of strings, each with a key, followed by an equals sign, followed by a value. We can use a list comprehension to iterate over the entire list and split each string into two strings based on the first equals sign. The optional second argument to the split() method is the number of times you want to split. 1 means “only split once,” so the split() method will return a two-item list. (In theory, a value could contain an equals sign too. If you just used 'key=value=foo'.split('='), you would end up with a three-item list ['key', 'value', 'foo'].)
  5. Finally, Python can turn that list-of-lists into a dictionary simply by passing it to the dict() function.