summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEzio Melotti <ezio.melotti@gmail.com>2009-09-26 11:20:53 (GMT)
committerEzio Melotti <ezio.melotti@gmail.com>2009-09-26 11:20:53 (GMT)
commit9aac2455ab9b47033b8b0807e28a3d8b4a9d9d37 (patch)
tree44ea056442ffde8359132c38fd1243ada17acf49
parentafdbe3d661c8abbbcb8737a85d1289ae135579e4 (diff)
downloadcpython-9aac2455ab9b47033b8b0807e28a3d8b4a9d9d37.zip
cpython-9aac2455ab9b47033b8b0807e28a3d8b4a9d9d37.tar.gz
cpython-9aac2455ab9b47033b8b0807e28a3d8b4a9d9d37.tar.bz2
#7000: document "sep" in capwords. Add a few tests
-rw-r--r--Doc/library/string.rst14
-rw-r--r--Lib/string.py10
-rw-r--r--Lib/test/test_string.py3
3 files changed, 17 insertions, 10 deletions
diff --git a/Doc/library/string.rst b/Doc/library/string.rst
index 52aa41c..9a13dc6 100644
--- a/Doc/library/string.rst
+++ b/Doc/library/string.rst
@@ -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)
diff --git a/Lib/string.py b/Lib/string.py
index e4d9e4f..6faa1f0 100644
--- a/Lib/string.py
+++ b/Lib/string.py
@@ -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
diff --git a/Lib/test/test_string.py b/Lib/test/test_string.py
index 35ab117..7947e43 100644
--- a/Lib/test/test_string.py
+++ b/Lib/test/test_string.py
@@ -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()