Skip to content

Commit b015fc8

Browse files
skrunglyzooba
authored andcommitted
bpo-36549: str.capitalize now titlecases the first character instead of uppercasing it (GH-12804)
1 parent f13c5c8 commit b015fc8

File tree

5 files changed

+10
-5
lines changed

5 files changed

+10
-5
lines changed

Doc/library/stdtypes.rst

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1509,6 +1509,10 @@ expression support in the :mod:`re` module).
15091509
Return a copy of the string with its first character capitalized and the
15101510
rest lowercased.
15111511

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

15131517
.. method:: str.casefold()
15141518

@@ -2052,8 +2056,7 @@ expression support in the :mod:`re` module).
20522056
>>> import re
20532057
>>> def titlecase(s):
20542058
... return re.sub(r"[A-Za-z]+('[A-Za-z]+)?",
2055-
... lambda mo: mo.group(0)[0].upper() +
2056-
... mo.group(0)[1:].lower(),
2059+
... lambda mo: mo.group(0).capitalize(),
20572060
... s)
20582061
...
20592062
>>> titlecase("they're bill's friends.")

Lib/test/string_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,7 +977,7 @@ def test_hash(self):
977977
def test_capitalize_nonascii(self):
978978
# check that titlecased chars are lowered correctly
979979
# \u1ffc is the titlecased char
980-
self.checkequal('\u03a9\u0399\u1ff3\u1ff3\u1ff3',
980+
self.checkequal('\u1ffc\u1ff3\u1ff3\u1ff3',
981981
'\u1ff3\u1ff3\u1ffc\u1ffc', 'capitalize')
982982
# check with cased non-letter chars
983983
self.checkequal('\u24c5\u24e8\u24e3\u24d7\u24de\u24dd',

Lib/test/test_unicode.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -811,7 +811,7 @@ def test_capitalize(self):
811811
self.assertEqual('h\u0130'.capitalize(), 'H\u0069\u0307')
812812
exp = '\u0399\u0308\u0300\u0069\u0307'
813813
self.assertEqual('\u1fd2\u0130'.capitalize(), exp)
814-
self.assertEqual('finnish'.capitalize(), 'FInnish')
814+
self.assertEqual('finnish'.capitalize(), 'Finnish')
815815
self.assertEqual('A\u0345\u03a3'.capitalize(), 'A\u0345\u03c2')
816816

817817
def test_title(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Change str.capitalize to use titlecase for the first character instead of
2+
uppercase.

Objects/unicodeobject.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9675,7 +9675,7 @@ do_capitalize(int kind, void *data, Py_ssize_t length, Py_UCS4 *res, Py_UCS4 *ma
96759675
Py_UCS4 c, mapped[3];
96769676

96779677
c = PyUnicode_READ(kind, data, 0);
9678-
n_res = _PyUnicode_ToUpperFull(c, mapped);
9678+
n_res = _PyUnicode_ToTitleFull(c, mapped);
96799679
for (j = 0; j < n_res; j++) {
96809680
*maxchar = Py_MAX(*maxchar, mapped[j]);
96819681
res[k++] = mapped[j];

0 commit comments

Comments
 (0)