Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a40bdda

Browse files
committed
Merged revisions 75070 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r75070 | ezio.melotti | 2009-09-26 14:20:53 +0300 (Sat, 26 Sep 2009) | 1 line #7000: document "sep" in capwords. Add a few tests ........
1 parent 2c6a949 commit a40bdda

3 files changed

Lines changed: 17 additions & 10 deletions

File tree

Doc/library/string.rst

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -565,10 +565,12 @@ rule:
565565
Helper functions
566566
----------------
567567

568-
.. function:: capwords(s)
569-
570-
Split the argument into words using :func:`split`, capitalize each word using
571-
:func:`capitalize`, and join the capitalized words using :func:`join`. Note
572-
that this replaces runs of whitespace characters by a single space, and removes
573-
leading and trailing whitespace.
568+
.. function:: capwords(s[, sep])
569+
570+
Split the argument into words using :meth:`str.split`, capitalize each word
571+
using :meth:`str.capitalize`, and join the capitalized words using
572+
:meth:`str.join`. If the optional second argument *sep* is absent
573+
or ``None``, runs of whitespace characters are replaced by a single space
574+
and leading and trailing whitespace are removed, otherwise *sep* is used to
575+
split and join the words.
574576

Lib/string.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,17 @@
2929

3030
# Capitalize the words in a string, e.g. " aBc dEf " -> "Abc Def".
3131
def capwords(s, sep=None):
32-
"""capwords(s, [sep]) -> string
32+
"""capwords(s [,sep]) -> string
3333
3434
Split the argument into words using split, capitalize each
3535
word using capitalize, and join the capitalized words using
36-
join. Note that this replaces runs of whitespace characters by
37-
a single space.
36+
join. If the optional second argument sep is absent or None,
37+
runs of whitespace characters are replaced by a single space
38+
and leading and trailing whitespace are removed, otherwise
39+
sep is used to split and join the words.
3840
3941
"""
40-
return (sep or ' ').join([x.capitalize() for x in s.split(sep)])
42+
return (sep or ' ').join(x.capitalize() for x in s.split(sep))
4143

4244

4345
####################################################################

Lib/test/test_string.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ def test_capwords(self):
2222
self.assertEqual(string.capwords('ABC DEF GHI'), 'Abc Def Ghi')
2323
self.assertEqual(string.capwords('ABC-DEF-GHI', '-'), 'Abc-Def-Ghi')
2424
self.assertEqual(string.capwords('ABC-def DEF-ghi GHI'), 'Abc-def Def-ghi Ghi')
25+
self.assertEqual(string.capwords(' aBc DeF '), 'Abc Def')
26+
self.assertEqual(string.capwords('\taBc\tDeF\t'), 'Abc Def')
27+
self.assertEqual(string.capwords('\taBc\tDeF\t', '\t'), '\tAbc\tDef\t')
2528

2629
def test_formatter(self):
2730
fmt = string.Formatter()

0 commit comments

Comments
 (0)