Skip to content

Commit

Permalink
bpo-36549: str.capitalize now titlecases the first character instead …
Browse files Browse the repository at this point in the history
…of uppercasing it (GH-12804)
  • Loading branch information
skrungly authored and zooba committed Apr 12, 2019
1 parent f13c5c8 commit b015fc8
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 5 deletions.
7 changes: 5 additions & 2 deletions Doc/library/stdtypes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1509,6 +1509,10 @@ expression support in the :mod:`re` module).
Return a copy of the string with its first character capitalized and the
rest lowercased.

.. versionchanged:: 3.8
The first character is now put into titlecase rather than uppercase.
This means that characters like digraphs will only have their first
letter capitalized, instead of the full character.

.. method:: str.casefold()

Expand Down Expand Up @@ -2052,8 +2056,7 @@ expression support in the :mod:`re` module).
>>> import re
>>> def titlecase(s):
... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
... lambda mo: mo.group(0)[0].upper() +
... mo.group(0)[1:].lower(),
... lambda mo: mo.group(0).capitalize(),
... s)
...
>>> titlecase("they're bill's friends.")
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/string_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -977,7 +977,7 @@ def test_hash(self):
def test_capitalize_nonascii(self):
# check that titlecased chars are lowered correctly
# \u1ffc is the titlecased char
self.checkequal('\u03a9\u0399\u1ff3\u1ff3\u1ff3',
self.checkequal('\u1ffc\u1ff3\u1ff3\u1ff3',
'\u1ff3\u1ff3\u1ffc\u1ffc', 'capitalize')
# check with cased non-letter chars
self.checkequal('\u24c5\u24e8\u24e3\u24d7\u24de\u24dd',
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -811,7 +811,7 @@ def test_capitalize(self):
self.assertEqual('h\u0130'.capitalize(), 'H\u0069\u0307')
exp = '\u0399\u0308\u0300\u0069\u0307'
self.assertEqual('\u1fd2\u0130'.capitalize(), exp)
self.assertEqual('finnish'.capitalize(), 'FInnish')
self.assertEqual('finnish'.capitalize(), 'Finnish')
self.assertEqual('A\u0345\u03a3'.capitalize(), 'A\u0345\u03c2')

def test_title(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Change str.capitalize to use titlecase for the first character instead of
uppercase.
2 changes: 1 addition & 1 deletion Objects/unicodeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -9675,7 +9675,7 @@ do_capitalize(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *ma
Py_UCS4 c, mapped[3];

c = PyUnicode_READ(kind, data, 0);
n_res = _PyUnicode_ToUpperFull(c, mapped);
n_res = _PyUnicode_ToTitleFull(c, mapped);
for (j = 0; j < n_res; j++) {
*maxchar = Py_MAX(*maxchar, mapped[j]);
res[k++] = mapped[j];
Expand Down

0 comments on commit b015fc8

Please sign in to comment.