]> granicus.if.org Git - python/commitdiff
bpo-36549: str.capitalize now titlecases the first character instead of uppercasing...
authorKingsley M <37349466+kingdom5500@users.noreply.github.com>
Fri, 12 Apr 2019 15:35:39 +0000 (16:35 +0100)
committerSteve Dower <steve.dower@microsoft.com>
Fri, 12 Apr 2019 15:35:39 +0000 (08:35 -0700)
Doc/library/stdtypes.rst
Lib/test/string_tests.py
Lib/test/test_unicode.py
Misc/NEWS.d/next/Core and Builtins/2019-04-11-12-41-31.bpo-36549.QSp8of.rst [new file with mode: 0644]
Objects/unicodeobject.c

index bae989e6b3a9fa75164461252bb741de27c16423..aeecdbb24a57b1fbd05be5e5aa16a2fb40e19f24 100644 (file)
@@ -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()
 
@@ -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.")
index 561b09a2d5ee8309dc3b91390dd516a2292b337e..836a43b81dd6f0da7ba163daee3ddc5fb099fdc1 100644 (file)
@@ -977,7 +977,7 @@ class CommonTest(BaseTest):
     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',
index 1131efdd26abcecad17de7c721db6b6696fba4e9..36b72e40c7e4193962c6aceac9a1194344fcd369 100644 (file)
@@ -811,7 +811,7 @@ class UnicodeTest(string_tests.CommonTest,
         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):
diff --git a/Misc/NEWS.d/next/Core and Builtins/2019-04-11-12-41-31.bpo-36549.QSp8of.rst b/Misc/NEWS.d/next/Core and Builtins/2019-04-11-12-41-31.bpo-36549.QSp8of.rst
new file mode 100644 (file)
index 0000000..9c6834c
--- /dev/null
@@ -0,0 +1,2 @@
+Change str.capitalize to use titlecase for the first character instead of
+uppercase.
index c0b345be7e8d3ba75f2627a7b5d3df5e106b82c8..e00dc37974f8e871897b24fa548f8b9f450b24ba 100644 (file)
@@ -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];