[Python-checkins] r75070 - in python/trunk: Doc/library/string.rst Lib/string.py Lib/test/test_string.py

ezio.melotti python-checkins at python.org
Sat Sep 26 13:20:58 CEST 2009


Author: ezio.melotti
Date: Sat Sep 26 13:20:53 2009
New Revision: 75070

Log:
#7000: document "sep" in capwords. Add a few tests

Modified:
   python/trunk/Doc/library/string.rst
   python/trunk/Lib/string.py
   python/trunk/Lib/test/test_string.py

Modified: python/trunk/Doc/library/string.rst
==============================================================================
--- python/trunk/Doc/library/string.rst	(original)
+++ python/trunk/Doc/library/string.rst	Sat Sep 26 13:20:53 2009
@@ -585,12 +585,14 @@
 They are not available as string methods.
 
 
-.. function:: capwords(s)
+.. function:: capwords(s[, sep])
 
-   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.
+   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)

Modified: python/trunk/Lib/string.py
==============================================================================
--- python/trunk/Lib/string.py	(original)
+++ python/trunk/Lib/string.py	Sat Sep 26 13:20:53 2009
@@ -43,15 +43,17 @@
 
 # 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

Modified: python/trunk/Lib/test/test_string.py
==============================================================================
--- python/trunk/Lib/test/test_string.py	(original)
+++ python/trunk/Lib/test/test_string.py	Sat Sep 26 13:20:53 2009
@@ -105,6 +105,9 @@
         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()


More information about the Python-checkins mailing list