]> granicus.if.org Git - python/commitdiff
#7000: document "sep" in capwords. Add a few tests
authorEzio Melotti <ezio.melotti@gmail.com>
Sat, 26 Sep 2009 11:20:53 +0000 (11:20 +0000)
committerEzio Melotti <ezio.melotti@gmail.com>
Sat, 26 Sep 2009 11:20:53 +0000 (11:20 +0000)
Doc/library/string.rst
Lib/string.py
Lib/test/test_string.py

index 52aa41c12dc10a5900c7160feffba4003b016787..9a13dc6667a1c5065882f32f2a5b15e64c082767 100644 (file)
@@ -585,12 +585,14 @@ The following functions are available to operate on string and Unicode objects.
 They are not available as string methods.
 
 
-.. function:: capwords(s)
-
-   Split the argument into words using :func:`split`, capitalize each word using
-   :func:`capitalize`, and join the capitalized words using :func:`join`.  Note
-   that this replaces runs of whitespace characters by a single space, and removes
-   leading and trailing whitespace.
+.. function:: capwords(s[, sep])
+
+   Split the argument into words using :meth:`str.split`, capitalize each word
+   using :meth:`str.capitalize`, and join the capitalized words using
+   :meth:`str.join`.  If the optional second argument *sep* is absent
+   or ``None``, runs of whitespace characters are replaced by a single space
+   and leading and trailing whitespace are removed, otherwise *sep* is used to
+   split and join the words.
 
 
 .. function:: maketrans(from, to)
index e4d9e4f21f6087df7d96fe0622ce6141809427ae..6faa1f02c81e1024200a7a2941d15b729ea2e261 100644 (file)
@@ -43,15 +43,17 @@ del l
 
 # Capitalize the words in a string, e.g. " aBc  dEf " -> "Abc Def".
 def capwords(s, sep=None):
-    """capwords(s, [sep]) -> string
+    """capwords(s [,sep]) -> string
 
     Split the argument into words using split, capitalize each
     word using capitalize, and join the capitalized words using
-    join. Note that this replaces runs of whitespace characters by
-    a single space.
+    join.  If the optional second argument sep is absent or None,
+    runs of whitespace characters are replaced by a single space
+    and leading and trailing whitespace are removed, otherwise
+    sep is used to split and join the words.
 
     """
-    return (sep or ' ').join([x.capitalize() for x in s.split(sep)])
+    return (sep or ' ').join(x.capitalize() for x in s.split(sep))
 
 
 # Construct a translation string
index 35ab117df456641dabc17efd234198ba4416286f..7947e43e9efdc64b556da3863796fa7716e34ac9 100644 (file)
@@ -105,6 +105,9 @@ class ModuleTest(unittest.TestCase):
         self.assertEqual(string.capwords('ABC DEF GHI'), 'Abc Def Ghi')
         self.assertEqual(string.capwords('ABC-DEF-GHI', '-'), 'Abc-Def-Ghi')
         self.assertEqual(string.capwords('ABC-def DEF-ghi GHI'), 'Abc-def Def-ghi Ghi')
+        self.assertEqual(string.capwords('   aBc  DeF   '), 'Abc Def')
+        self.assertEqual(string.capwords('\taBc\tDeF\t'), 'Abc Def')
+        self.assertEqual(string.capwords('\taBc\tDeF\t', '\t'), '\tAbc\tDef\t')
 
     def test_formatter(self):
         fmt = string.Formatter()